I need to get configuration from server before the application bootstraps. I was able to do that using this in providers in my main ngModule:
{
provide: APP_INITIALIZER,
useFactory: (links: Links) => () => links.init(),
deps: [Links, Http],
multi: true
}
Links service:
init(): Promise<any> {
var observable = this.http.get('someUrl').map(res => res.json());
observable.subscribe(res => {
this.config = res;
});
return observable.toPromise();
}
This code gets executed before the application bootstraps, but the response from server is not present until after my app asks for Links.config. How do I force the app to not bootstrap until after the Promise gets resolved? I tried promise.resolve(), but it didn't help.
The way I used providers I thought I forced the app to use the same instance of Links and also I thought the data would be already present there. What am I doing wrong please?
In my code I am doing exactly same and it's working only difference is that I am converting the request to Promise before mapping.
public load(): Promise<void> {
let headers = new Headers({ 'Content-Type': 'application/json' });
let url = this.urls.settings;
return this.http.get(url, { headers }).toPromise().
then(configs => {
this.startupSettings = configs.json();
}).catch(err => {
log(err);
});
}
...............................
{
provide: APP_INITIALIZER,
useFactory: (config: StartupConfigurationService) => () => config.load(),
deps: [StartupConfigurationService],
multi: true
}
I don't sure it will make sense but try may be it will solve
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With