diff --git a/packages/core/src/application_init.ts b/packages/core/src/application_init.ts index b89ed0908a..69b6fe473d 100644 --- a/packages/core/src/application_init.ts +++ b/packages/core/src/application_init.ts @@ -27,6 +27,59 @@ import {noop} from './util/noop'; * * @see `ApplicationInitStatus` * + * @usageNotes + * + * The following example illustrates how to configure a multi-provider using `APP_INITIALIZER` token + * and a function returning a promise. + * + * ``` + * function initializeApp(): Promise { + * return new Promise((resolve, reject) => { + * // Do some asynchronous stuff + * resolve(); + * }); + * } + * + * @NgModule({ + * imports: [BrowserModule], + * declarations: [AppComponent], + * bootstrap: [AppComponent], + * providers: [{ + * provide: APP_INITIALIZER, + * useFactory: () => initializeApp, + * multi: true + * }] + * }) + * export class AppModule {} + * ``` + * + * It's also possible to configure a multi-provider using `APP_INITIALIZER` token and a function + * returning an observable, see an example below. Note: the `HttpClient` in this example is used for + * demo purposes to illustrate how the factory function can work with other providers available + * through DI. + * + * ``` + * function initializeApp(httpClient: HttpClient): Observable { + * return httpClient.get("https://someUrl.com/api/user") + * .pipe( + * tap(user => { ... }) + * ) + * } + * + * @NgModule({ + * imports: [BrowserModule, HttpClientModule], + * declarations: [AppComponent], + * bootstrap: [AppComponent], + * providers: [{ + * provide: APP_INITIALIZER, + * useFactory: initializeApp, + * deps: [HttpClient], + * multi: true + * }] + * }) + * export class AppModule {} + * ``` + * * @publicApi */ export const APP_INITIALIZER =