Intercept observables before subscription callback

3631 views rxjs
7

I am using the following code to make get request:

    makeGetReq$(url:string):Observable{
        let getReqObservable;
        getReqObservable = this.httpClient.get(url) //code for making get request
        return getReqObservable
    }

The problem is sometimes my backend might return {error:true, message} with status code 200. (I know thats weird).In that case I want to intecept getReqObservable and not allow its subscription callback to run.

image.component.ts

    makeGetReq$(url:string):Observable{
         let getReqObservable;
         getReqObservable = this.httpClient.get(url)//code for making get request
         return getReqObservable
                    .do((value)=>{
                       if(value.error){
                       //do not allow it to propagate further
                      })

      })

answered question

1 Answer

5

The easiest would probably be filter.

Filter items emitted by the source Observable by only emitting those that satisfy a specified predicate.

It would look like this:

return getReqObservable
    .filter(value => !value.error)

posted this

Have an answer?

JD

Please login first before posting an answer.