1

I am new to rxjs operators

What is the most efficient way to execute two requests and check if one of the requests comes back as valid and also check if both failed.

I tried forkJoin but unable to determine how to when both requests failed.

Immortal
  • 1,233
  • 4
  • 20
  • 47

2 Answers2

1

There is not true, you can use forkjoin in this way:

this.forkJoinRequest= forkJoin([this.service.getMethod1(), this.service.getMethod2()]).subscribe(response => {
                  this.list1= response[0];
                  this.list2=response[1];
 },
                  (error) => { this.error_code = error.status; });

you can use your "error" to show why the forkjoin is failed.

Doflamingo19
  • 1,591
  • 4
  • 12
  • 32
1

Well, forkJoin is probably what you're looking for but if you want to be able to handle both errors you'll need to chain each source with catchError and return errors as next notifications.

Otherwise one failed source Observable would dispose the chain and you'd never know whether the second Observable failed as well or which one of the two Observables failed.

forkJoin([
  source1$.pipe(catchError(error => of(error))),
  source2$.pipe(catchError(error => of(error))),
]).subscribe(([reponse1, response2]) => {
  if (reponse1 instanceof ErrorResponse || reponse2 instanceof ErrorResponse) {
     // or whatever works for you
  }
});
martin
  • 93,354
  • 25
  • 191
  • 226