Skip to content

Commit cf5f863

Browse files
abarghoudAndrewKushnir
authored andcommitted
docs(core): add usage examples for APP_INITIALIZER token (angular#41095)
Add multiple usage examples for APP_INITIALIZER using Promise, Observable and multi providers Closes angular#40730 PR Close angular#41095
1 parent fed6a7c commit cf5f863

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

packages/core/src/application_init.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,59 @@ import {noop} from './util/noop';
2727
*
2828
* @see `ApplicationInitStatus`
2929
*
30+
* @usageNotes
31+
*
32+
* The following example illustrates how to configure a multi-provider using `APP_INITIALIZER` token
33+
* and a function returning a promise.
34+
*
35+
* ```
36+
* function initializeApp(): Promise<any> {
37+
* return new Promise((resolve, reject) => {
38+
* // Do some asynchronous stuff
39+
* resolve();
40+
* });
41+
* }
42+
*
43+
* @NgModule({
44+
* imports: [BrowserModule],
45+
* declarations: [AppComponent],
46+
* bootstrap: [AppComponent],
47+
* providers: [{
48+
* provide: APP_INITIALIZER,
49+
* useFactory: () => initializeApp,
50+
* multi: true
51+
* }]
52+
* })
53+
* export class AppModule {}
54+
* ```
55+
*
56+
* It's also possible to configure a multi-provider using `APP_INITIALIZER` token and a function
57+
* returning an observable, see an example below. Note: the `HttpClient` in this example is used for
58+
* demo purposes to illustrate how the factory function can work with other providers available
59+
* through DI.
60+
*
61+
* ```
62+
* function initializeApp(httpClient: HttpClient): Observable<any> {
63+
* return httpClient.get("https://someUrl.com/api/user")
64+
* .pipe(
65+
* tap(user => { ... })
66+
* )
67+
* }
68+
*
69+
* @NgModule({
70+
* imports: [BrowserModule, HttpClientModule],
71+
* declarations: [AppComponent],
72+
* bootstrap: [AppComponent],
73+
* providers: [{
74+
* provide: APP_INITIALIZER,
75+
* useFactory: initializeApp,
76+
* deps: [HttpClient],
77+
* multi: true
78+
* }]
79+
* })
80+
* export class AppModule {}
81+
* ```
82+
*
3083
* @publicApi
3184
*/
3285
export const APP_INITIALIZER =

0 commit comments

Comments
 (0)