I am attempting to establish an opaque token in the providers using an observable. The purpose behind this is that I am retrieving the value through the Http provider (from an external JSON file).
This is my current approach:
{
provide: SOME_OPAQUE_TOKEN,
useFactory: (configService: ConfigService) => {
configService.getPath('campaigns')
.subscribe((res) => {
???
});
},
deps: [ConfigService],
},
It's evident that this method will not work, so I'm curious if there is a solution to tackle this issue?
Is it feasible to construct a service using useFactory where one of the parameters is fetched asynchronously?
Does such a possibility exist?
Edit: Solution utilizing APP_INITIALIZER
In AppModule:
{
provide: APP_INITIALIZER,
useFactory: (configService: ConfigService) => () => configService.load(),
multi: true,
deps: [ConfigService, Http],
},
In ConfigService load():
public load(): Promise<Configuration> {
let promise =
this.http
.get('config.json')
.map(m => m.json())
.toPromise();
promise
.then(config => this.appConfig = config);
return promise;
}
Once the appConfig is set, we can utilize it to define the OpaqueToken:
{
provide: BASE_PATH,
useFactory: (configService: ConfigService) => configService.appConfig.basePath, deps: [ConfigService],
},