Search code examples
angularrxjsrxjs-observablesrxjs-pipe

Angular and RxJS - subscribe function for forkjoin not executing


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 had its own subscribe so it was not passing an Observable. Once I changed that, the forkJoin successfully invoked the subscribe function.


Solution

  • 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.