Search code examples
angularrxjsangular-http-interceptorsangular-httpclient

angular http interceptor - abort response


Is there a way to use angular's http interceptors to intercept the response, and kinda delete it / throw it away, so that future downstream callbacks aren't executed?

I want it to behave such that neither of the console.log's are executed.

this.http.get('/foo').subscribe(
    data => console.log("success", data),
    err => console.log("fail.", err)
);

I've seen examples that modify the response, or replace the response with null or some other sentinel value, but I'd rather not do that because then all of my success/fail handlers must look for the sentinel, which reduces the usefulness of using an interceptor for handling certain responses.

I have a feeling this is more of an rxjs question and less of an angular interceptor question, but I'm just not familiar enough with rx yet to be sure.

If it matters, I'm using angular 5.1


Solution

  • You can use Observable.empty to complete the stream without emitting any data. To combine it with HttpInterceptor, chain it to next.handle:

    return next.handle(req).switchMap(() => Observable.empty());
    

    I don't know how to do that with Promise, sorry.