Karthik G
Karthik G

Reputation: 1262

execute multiple requests angular 8

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.

Upvotes: 1

Views: 410

Answers (2)

martin
martin

Reputation: 96969

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
  }
});

Upvotes: 1

Doflamingo19
Doflamingo19

Reputation: 1639

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.

Upvotes: 1

Related Questions