New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Aborting a fetch #95
Comments
The cancelable promises proposal was recently withdrawn (see tc39/proposal-cancelable-promises#70 and whatwg/fetch#27). New tracking issue: whatwg/fetch#447. |
The spec is (semi-)ready. https://developer.mozilla.org/en-US/docs/Web/API/FetchController |
Everyone, |
@TimothyGu Thx for the heads-up, I feel sorry for not being able to help much as I am fighting on multiple fronts. Time to see if there are any takers on this issue (aka #252). |
Here are the tests for fetch ran in browser (if you want to check on browser implementation progress) http://wpt.fyi/fetch/api/abort |
The Web Platform Tests were added in web-platform-tests/wpt#6484. An overview of WHATWG's design of aborting fetch is available at web-platform-tests/wpt#6484 (comment). If it's not too hard we should look at integrating WPT into this module. IMO we should try to implement (or endorse) a version of |
That polyfill is depending on browser due to extending DOM's event emiter. I could release a independent module that mostly look like this: const EventEmitter = require('events')
class AbortSignal extends EventEmitter {
constructor() {
super()
this.aborted = false
}
}
class AbortController {
constructor() {
this.signal = new AbortSignal()
}
abort() {
this.signal.aborted = true
this.signal.emit('abort')
}
} It would feel a bit more nicer to have a node emitter also |
@jimmywarting Yeah it would feel more natural given node-fetch's server-side context. I've already filed mo/abortcontroller-polyfill#2. Regarding monkey patching |
I will make a such package now and probably call it |
the abortcontroller package don't have to do anything with regards to fetch, it's just node-fetch that has to use it correctly |
For what it's worth, one way to cancel a Fetch in-browser is to consume the body as a stream and then close the stream. Calling |
Good to know, although you would only be able to abort the download, not the upload |
While working on the browser mo/abortcontroller-polyfill#12 While you could have nicer implementations for browser (using I'm also of the opinion this should only polyfill |
A WIP is now available in #437. |
I can confirm that #437 fixes my issue in #438. However, a few questions:
|
Does anyone have sample code for the WIP? Is this the API? const controller = new AbortController();
const signal = controller.signal;
controller.abort();
fetch(Request("http://api.github.com", {signal}))
.then(r => r.json())
.then(j => console.log(j))
.catch(err => {
if (err.name === 'AbortError') {
console.log('aborted');
}
}) |
Alright, found the http.request.abort() line that should also abort the response stream. https://github.com/bitinn/node-fetch/blob/a823a1ef74a34b9b6b25ed6cb790de8c40e4ef38/src/index.js#L68 |
Hi @TimothyGu, I am trying to abort a slow const controller = new AbortController()
const res = await fetch(url, {
signal: controller.signal,
}).then((res) => {
res.abortController = controller
return res
})
setImmediate(() => res.abortController.abort())
res.body.on('error') # nothing is caught
res.body.on('abort') # nothing is caught
await res.json() |
A shoutout to people watching this thread, we are closed to merging AbortSignal support, it's a bit different from previous WIP, so let us know if you have any feedback on it: #539 |
See README for details and examples. |
What about Pausing/Resuimg a download? |
maybe through2 might be helpful for doing that? |
Just looked at though2 and can't think how i could use it. I have a question here about it pausing/resuming a download. |
Implement
AbortController
so we can abort a requestThe text was updated successfully, but these errors were encountered: