Skip to content

How does switchMap cancel HTTP requests #6975

Answered by voliva
jtkb asked this question in Q&A
Discussion options

You must be logged in to vote

switchMap just calls .unsubscribe() to the observable returned by the map function when a new value comes in.

If the observable returned by the map function cancels the HTTP request when calling .unsubscribe() on it, then the call will be canceled. If it doesn't, then it won't.

So I expect that switchMap(() => fetch('https://...')) will not cancel it - fetch returns a promise, which is not cancelable. It will get wrapped in an observable that on unsubscribe does nothing.

Angular's Http does internally use XHR to perform the actual request, and you can see it aborting the request in the teardown function of the observable here: https://github.com/angular/angular/blob/main/packages/common/h…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@jtkb
Comment options

@baoqger
Comment options

@jakovljevic-mladen
Comment options

Answer selected by jtkb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants