question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fromFetch cannot abort all responses

See original GitHub issue

Bug Report Feature Request

Current Behavior

fromFetch returns an observable that emits the request’s Response object. When json() or text() is called on the Response, a promise is returned. My understanding is that the AbortController used in the fetch call also aborts promises returned by the aforementioned methods.

In #4742, a bug was fixed: fromFetch was calling abort as part of the implicit unsubscribe that occurs after completion. This would abort any still pending responses. When fetch resolves, a flag is now set to prevent abort being called in the unsubscribe teardown.

However, that means there is no way of aborting the promises returned by calls to Response methods.

Possible Solution

Add another signature to fromFetch so that a selector can be passed:

export function fromFetch<R>(
  input: string | Request,
  init: RequestInit | undefined,
  selector: (response: Response) => Observable<R>,
): Observable<R>;

Much like the selector passed to multicast, observables composed within the selector will be ‘contained’ within the observable returned by fromFetch and unsubscribing from said observable will facilitate the aborting of any promises returned by Response methods within the selector.

If a selector is not passed, fromFetch should continue to behave as in #4742 and not abort upon unsubscription if the promise returned by fetch has resolved.

Additional Information

Note that the changes introduced in https://github.com/ReactiveX/rxjs/pull/3963/files will ensure that unsubscription from the observable returned by fromFetch will occur ASAP if said observable serves as the source for, say, concatMap. So extending the ‘life’ of the AbortController for an emitted Response - if the selector is not used - would be a non-trivial undertaking.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
curiousercreativecommented, May 27, 2020

@cartant thanks for that follow-up. My implementation actually doesn’t have to wait for the next release. In my case, I’m streaming my chunked response using response.body.getReader() and after reading through your article and this MDN reference, I found that I can simply cancel the reader stream when my custom Observable unsubscribes (from a switchMap). Works like a charm, but I wouldn’t have gotten it there without your help!

1reaction
cartantcommented, May 27, 2020

@curiousercreative BTW, if you are interested, the reason for the selector is explained here: https://ncjamieson.com/understanding-fromfetch/

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to Fetch Response - 201 Message - Stack Overflow
All I get from fetch is response 201 message that it's created. When I run the API in postman I get the response:...
Read more >
fromFetch - RxJS
Unsubscribing from any observable that uses the promise as an observable input will not abort the request. To facilitate aborting the retrieval of...
Read more >
Fetch: Abort - The Modern JavaScript Tutorial
As we know, fetch returns a promise. And JavaScript generally has no concept of “aborting” a promise. So how can we cancel an...
Read more >
fetch() - Web APIs | MDN
The promise resolves to the Response object representing the ... with a fetch request and abort it if desired via an AbortController ....
Read more >
Mastering RxJS: operators and functions that can bite you ...
fromFetch function — uses the Fetch API to make an HTTP request. ... forkJoin waits for all argument Observables to complete and then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found