In the code below, the subscribe function is not run ('Never called' does not appear in the console); however, the three services are called.
ngOnInit(): void {
forkJoin([this.adminBCService.getAvailableBusinessCases(), this.explainersService.get(), this.adminPainsService.getMetricsList()])
.pipe(takeUntil(this.destroy$))
.subscribe(resultsArray => {
console.log(resultsArray);
this.businessCasesSelectable = resultsArray[0];
this.selectedBusinessCase = this.businessCasesSelectable.length ? this.businessCasesSelectable[0] : null;
this.explainers = resultsArray[1];
this.metricList = resultsArray[2];
});
}
I'm very new to RxJS and Angular. I need to call an additional service after the first three complete and was planning to do this in the subscribe() function off of the forkjoin but something I've done seems to prevent that function from doing anything.
UPDATE: the third service call executed successfully but had an empty result. This appeared to be the cause.
CodePudding user response:
forkJoin
will only return a value if every observable has return at least one value and has completed.
This means either:
- One service call failed
- One service call didn't complete.
If you don't want to wait for a completion combineLatest
is a good alternative.
Also on a side note : Don't break out of the reactive context by affecting values a tap
: map
those values instead and set the class members in the final subscribe
.