allow returning promise
See original GitHub issue**I’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report <!-- Please check the repository for a similar issue or PR before submitting -->
[ ] Support request => <!-- Please check the repository for a similar issue or PR before submitting -->
[x] Feature request
[ ] Documentation issue or request
Current behavior
- Using an Angular app (v. 5.2.6), import the following into app.module:
ConfigModule.forRoot({
deps: [HttpClient],
provide: ConfigLoader,
useFactory: configFactory,
}),
where configFactory
is:
export function configFactory(http: HttpClient): ConfigLoader {
return new ConfigHttpLoader(http)
}
Then use getSettings
in a service:
@Injectable()
export class MyService {
constructor(private configService: ConfigService) {
const url = configService.getSettings("api.url", "")
}
}
url
would always be empty string, when in my config.json file was this:
{
"api": {
"url": "www.example.com/api"
}
}
This is mainly because the HttpLoader is most likely not finished with getting config.json by the time the constructor in my service calls getSettings
.
Expected/desired behavior
Same code, except have two methods: getSettingsSync
and getSettings
, where getSettings
returns a Promise or Observable when the service is finished with getting the settings.
Then in my code, I would go back to MyService
and do this:
constructor(private configService: ConfigService) {
const url = await configService.getSettings("api.url")
}
What is the motivation / use case for changing the behavior? It is nice to have the getSettings method to always return your expected configuration. Since it didn’t do this, it took me about 2-4 hours to realize why.
Environment
-
Angular version: 5.2.6
-
Browser:
- Chrome (desktop) version XX
- Chrome (Android) version XX
- Chrome (iOS) version XX
- Firefox version XX
- Safari (desktop) version XX
- Safari (iOS) version XX
- IE version XX
- Edge version XX
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (1 by maintainers)
Top GitHub Comments
@fulls1z3 I have the same or a similar problem.
Your HTTP interceptor will/might solve the problem when ConfigService is not yet initialized. It does not solve the problem with a call to GetSettings (within mergeMap) with a resolved ConfigService as the HTTP request initiated by the ConfigHttpLoader has not yet returned data.
I have a service, in the constructor I receive an initialized ConfigService, but the call to GetSettings fails, as no config is yet available. (I think the OP has the same problem).
Is there a possibility to wait for the HTTP request to finish before calling getSettings or have getSettings to wait until the config is available ?
As a stop gap solution
CustomConfigHttpLoader
in other app initializer