-
I'm curious as to how I know network requests using the fetch or XMLHttpRequest APIs using AbortController but this necessitates setting up the request with the signal from an AbortController. So far when I have need to do network requests withing a switchMap I have used Angular's Http class and network requests are automatically cancelled if I pass new values to a switch map. But how is this achieved as I am not also passing in an Abortcontroller? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
If the observable returned by the map function cancels the HTTP request when calling So I expect that 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/http/src/xhr.ts#L317 If you wanted to use fetch in the same way, you would need to use an implementation that will cancel it on .unsubscribe(). It seems |
Beta Was this translation helpful? Give feedback.
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…