Home > Software engineering >  Don't cancel the first observable if the second fails, how to keep it (forkJoin, zip). Angular
Don't cancel the first observable if the second fails, how to keep it (forkJoin, zip). Angular

Time:11-08

I have parallel API calls. How to continue getting data from one of them if second one failed?

forkJoin([a,b])
      .subscribe({
        next: ((data) => {
          const [first, second] = data;
        })

CodePudding user response:

It seems to me that combineLatest is what you are looking for, with a catchError to each of your observables to manage errors. Something like:

combineLatest([
  obs1$.pipe(catchError(() => of(null)),
  obs2$.pipe(catchError(() => of(null)),
]).subscribe(([first, second]) => { console.log(first,second); });

CodePudding user response:

I have already found a solution, we need to handle errors in each observable and return of().

catchError(() => of([]))
  • Related