Home > other >  What is the way in RxJS to keep collecting results from multiple and different http calls independen
What is the way in RxJS to keep collecting results from multiple and different http calls independen

Time:01-16

I've used zip and forkJoin

return forkJoin({
  a: this.getA(),
  b: this.getB(),
  c: this.getC()
})

the problem is that if one of those calls fails, all the call get cancelled, whereas I'd like to still collect data from the ones which didn't fail Is making them separately the only way to do it?

CodePudding user response:

To solve your problem, you have to catch the error and treat it correctly in the inner observable

return forkJoin({
  a: this.getA(),
  b: this.getB(),
  c: this.getC().pipe(catchError(error => of(error)))
})

// If getC fails, you would get A and B

My question is whether you have to add this treatment only to the last element or to all of them. You will have to try it yourself, please let me know in the comments.

See HERE the Example 5: Getting successful results when one inner observable errors

  •  Tags:  
  • Related