Rxjs concat map condition check before it go to second call

1k Views Asked by At

I want to check first service call result before proceeding further. If the first call fails, I have some thing to handle

    return this.http.get(this.partySiteAddressUrlOrganization + "?" + new Date().toString(), { params: newparams }).pipe(
  concatMap((result1: any) => {
    if (result1.success)
      return this.http.post(this.postRequestListURL, parm)
    else {
    }

  }));
1

There are 1 best solutions below

1
On BEST ANSWER

Not fully understand your question, base on my assumption here is what I think

You want to handle the error/exception if any thrown from your API.

You can simply use the catchError operator from rxjs/operators and write something like this.

return this.http.get(this.partySiteAddressUrlOrganization + "?" + new Date().toString(), { params: newparams })
.pipe(
  concatMap((result1: any) => {
      return this.http.post(this.postRequestListURL, parm)
    }),
catchError((error)=>{
 // handle the error
 // if you want to throw it the throw other wise send the EMPTY its up to you
 // using return EMPTY;
}));

If first one is not your scenario and you are saying your API will return a flag if something is not right then you can handle this like

return this.http.get(this.partySiteAddressUrlOrganization + "?" + new Date().toString(), { params: newparams })
    .pipe(
      concatMap((result1: any) => {
          if(result1.success) {
             return this.http.post(this.postRequestListURL, parm)
         } else {
          // handle your stuff here and the return 
           return of({}); // {} can be your object 
         }
        }),
    catchError((error)=>{
     // handle the error
     // if you want to throw it the throw other wise send the EMPTY its up to you
    }));