From cf5f86386ffceda4d051af09acb9dbded2d57172 Mon Sep 17 00:00:00 2001 From: abarghoud Date: Fri, 5 Mar 2021 23:03:16 +0100 Subject: [PATCH] docs(core): add usage examples for APP_INITIALIZER token (#41095) Add multiple usage examples for APP_INITIALIZER using Promise, Observable and multi providers Closes #40730 PR Close #41095 --- packages/core/src/application_init.ts | 53 +++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) 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 =