Angular 2 - APP_INITIALIZER : wait for chaining promises

Viewed 2569

I'm using the APP_INITIALIZER to load application config before everything else. This is working fine.

Now, I also have to get the current user by asking the server. BUT, the url of the server is in the application config (I cannot change that).

My question is :

How can I wait for all my promises to be achieved in the right order before loading the application ?

I tried this in my application module but the application starts right after the config is loaded, and doesn't wait for my user service.

providers: [
    { provide: APP_INITIALIZER, useFactory: loadConfigFactory, deps: [AppConfig, UserService], multi: true }
]

export function loadConfigFactory(config: AppConfig, userService: UserService) {
    return () => config.load().then((serverURL) => userService.setCurrentUser(serverURL));
}

How can I make sure that my config AND my user is set before everything else ?

EDIT:

I had forgot to add the "return" statement in the setCurrentUser promise. So it's solved.

I will not delete the post because it might help other people who wants to know how to wait for several promises to be done before loading the application.

0 Answers
Related