.asObservable don't want to work with Observable.forkJoin
I have a service:
export class ConfigService {
private _config: BehaviorSubject<object> = new BehaviorSubject(null);
public config: Observable<object> = this._config.asObservable();
constructor(private api: APIService) {
this.loadConfigs();
}
loadConfigs() {
this.api.get('/configs').subscribe( res => this._config.next(res) );
}
}
Trying to call this from a component:
...
Observable.forkJoin([someService.config])
.subscribe( res => console.log(res) ) //not working
someService.config.subscribe( res => console.log(res) ) // working
...
How can I use Observable.forkJoin
with a variable Observable
config
?
I need to keep the configs in the service and wait for them inactive and other requests are pending to stop the bootloader.
source to share
Since you're using BehaviorSubject
, you should know that you can call next()
and complete()
manually.
The operator forkJoin()
is emitted only when all its sources Observables emit at least one value , and they are all completed. Since you are using the Subject and method asObservable
, the source Observable never completes, and therefore the operator forkJoin
never emits anything.
Btw, it doesn't make sense to use forkJoin
with only one Observable source. Also maybe look at zip()
or combineLatest()
operators that are similar and maybe this is what you need.
Two very similar questions:
source to share