Skip to content

Commit

Permalink
docs(core): add an example of configuring APP_INITIALIZER token in co…
Browse files Browse the repository at this point in the history
…ntext of standalone components (#52084)

docs(core): fix formatting

PR Close #52084
  • Loading branch information
phalgunv authored and atscott committed Oct 10, 2023
1 parent 5411864 commit 644f3f2
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion packages/core/src/application_init.ts
Expand Up @@ -31,7 +31,7 @@ import {isPromise, isSubscribable} from './util/lang';
*
* The following example illustrates how to configure a multi-provider using `APP_INITIALIZER` token
* and a function returning a promise.
*
* ### Example with NgModule-based application
* ```
* function initializeApp(): Promise<any> {
* return new Promise((resolve, reject) => {
Expand All @@ -53,11 +53,38 @@ import {isPromise, isSubscribable} from './util/lang';
* export class AppModule {}
* ```
*
* ### Example with standalone application
* ```
* export function initializeApp(http: HttpClient) {
* return (): Promise<any> =>
* firstValueFrom(
* http
* .get("https://someUrl.com/api/user")
* .pipe(tap(user => { ... }))
* );
* }
*
* bootstrapApplication(App, {
* providers: [
* provideHttpClient(),
* {
* provide: APP_INITIALIZER,
* useFactory: initializeApp,
* multi: true,
* deps: [HttpClient],
* },
* ],
* });
* ```
*
*
* 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.
*
* ### Example with NgModule-based application
* ```
* function initializeAppFactory(httpClient: HttpClient): () => Observable<any> {
* return () => httpClient.get("https://someUrl.com/api/user")
Expand All @@ -80,6 +107,27 @@ import {isPromise, isSubscribable} from './util/lang';
* export class AppModule {}
* ```
*
* ### Example with standalone application
*
* function initializeAppFactory(httpClient: HttpClient): () => Observable<any> {
* return () => httpClient.get("https://someUrl.com/api/user")
* .pipe(
* tap(user => { ... })
* );
* }
*
* bootstrapApplication(App, {
* providers: [
* provideHttpClient(),
* {
* provide: APP_INITIALIZER,
* useFactory: initializeApp,
* multi: true,
* deps: [HttpClient],
* },
* ],
* });
*
* @publicApi
*/
export const APP_INITIALIZER =
Expand Down

0 comments on commit 644f3f2

Please sign in to comment.