Home > Back-end >  How can i catch the sucessfull response in rxjs and return some specific value?
How can i catch the sucessfull response in rxjs and return some specific value?

Time:09-29

I have the following code

let arrApi = [];

arrApi.push(this.securityService.deleteBusinessDataRule('1000').pipe(catchError(error => of({error: true}))))

forkJoin([...arrApi]).subscribe((forking: any) => {
            console.log('forking', forking);
        });

so I am building dynamically api request throught arrApi. The method (this.securityService.deleteBusinessDataRule('1000') returns observable. It is delete reqquest which get 204 as status code. If there is some error inside i am catching the error so in forking[0]

i will get error: true as a value because i catched the error in the observable and i will use it.

I don't know how can i catch the response when it is sucessfull ? So when the observable passes i want to emit value like {error: false} because this delete request does not return anything in the response after i hit the delete API and i keep getting undefined in forking[0].

So everytime when the observable is competed I need to get {error: false}

CodePudding user response:

You can just use map operator from rxjs:

arrApi.push(
  this.securityService.deleteBusinessDataRule('1000')
    .pipe(
      map(()=>({error: false})),
      catchError(error => of({error: true}))
  )
)
  • Related