1

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.

nstuyvesant
  • 1,392
  • 2
  • 18
  • 43
  • forkJoin waits for all of the sources to complete, and last values from all of 3 sources would be emmited as a result. If you want other behavior, you should use other functions, for example `combineLatest` or `zip` – Andrei Jan 11 '23 at 22:29

1 Answers1

2

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.

Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
  • Hmm... not seeing any HTTP errors and each service is returning data in the right format when I inspect the Network interactions. BTW, thanks for the suggestion... I updated the code above to set class members in the final subscribe. Still need to figure out which one is preventing the subscribe function from firing but I'll use process of elimination. – nstuyvesant Jan 11 '23 at 22:49