In my application there is angular 2 service lets say "Configuration Service" which returns RxJS Observable sequence. This sequence contains configuration data. On subscribing this sequence it returns data however data returned is asynchronously. In normal scenario asynchronous call may be desired however in one particular case I need to get this configuration data "synchronously" as without that configuration data returned from service execution flow can not continue. Is there any way I can make blocking call with observable sequence so that I can still use this existing configuration service? My code looks something like below:
this.configService.getConfigData()
.subscribe(
x=>{
this.ConfigData = x;
},
e=>console.log(e)
);
There is no way to turn things synchronously.
The approach you could consider is to load configuration data before bootstrapping your application. So they will be there when the application is started.
Here are the different possible approaches:
You could bootstrap asynchronously after your request(s) complete(s). Here is a sample:
var app = platform(BROWSER_PROVIDERS)
.application([BROWSER_APP_PROVIDERS, appProviders]);
var service = app.injector.get(ConfigService);
service.loadConfig().flatMap((config) => {
var configProvider = new Provider('config', { useValue: config });
return app.bootstrap(appComponentType, [ companiesProvider ]);
}).toPromise();
See this question:
I see another approach if you're able to update index.html page (the main entry point) on the server side. Here is a sample:
<script>
var params = {"token": "@User.Token", "xxx": "@User.Yyy"};
System.import('app/main').then((module) => {
module.main(params);
});
</script>
See this question:
You could also register an APP_INITIALIZER provider for this:
provide(APP_INITIALIZER, {
useFactory: (service:ConfigService) => () => service.loadConfig(),
deps:[ConfigService, HTTP_PROVIDERS],
multi: true}),
See this issue for more details:
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