Skip to content
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

add support for aborting via AbortController #54

Open
prabirshrestha opened this issue Sep 29, 2017 · 9 comments · May be fixed by #68 or #137
Open

add support for aborting via AbortController #54

prabirshrestha opened this issue Sep 29, 2017 · 9 comments · May be fixed by #68 or #137

Comments

@prabirshrestha
Copy link

https://developers.google.com/web/updates/2017/09/abortable-fetch

Currently it is only implemented in Firefox 57 and is coming to other browsers soon.

const controller = new AbortController();
const signal = controller.signal;

setTimeout(() => controller.abort(), 5000);

fetch(url, { signal }).then(response => {
  return response.text();
}).then(text => {
  console.log(text);
});

Will most likely need to implement another npm package that just include AbortController package. I found one but it also pollyfills fetch. https://github.com/mo/abortcontroller-polyfill

@developit
Copy link
Owner

So we'd need a new npm package that exports a standalone polyfill for AbortController, and then the only modifications needed to unfetch would be to proxy signal.onabort = request.abort. Seems like it might be doable in a few bytes.

@prabirshrestha
Copy link
Author

Awesome. Could you post the link to the npm package?

@Jador
Copy link

Jador commented Oct 30, 2017

@prabirshrestha
Copy link
Author

Chrome 66 beta seems to officially support it. https://blog.chromium.org/2018/03/chrome-66-beta-css-typed-object-model.html

simonbuerger added a commit to simonbuerger/unfetch that referenced this issue Apr 17, 2018
@simonbuerger simonbuerger linked a pull request Apr 17, 2018 that will close this issue
@lxsmnsyc
Copy link

An implementation of WHATWG AbortController interface.

https://github.com/mysticatea/abort-controller

@dangdennis
Copy link

dangdennis commented Nov 10, 2019

As of 2019, https://caniuse.com/#search=abort AbortController is supported by 86% of browser users. But I'm not sure if AbortController is available for use with XMLHttpRequest. But there's an alternative since that req object already has an .abort API.

So perhaps there can be a way expose the request's XMLHttpRequest.abort method to the caller from the promise wrapper. Although I'm unsure how you'd return that in the context of a Promise since resolve is only allowed to be called once. https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/abort

Edit:
Nevermind, someone already solved it using event emitters.
https://codesandbox.io/s/92vnjok08r

@JohnnyFun
Copy link

You could also just add an abort function on the returned promise. Hacky, but works.

function unfetch(url, options) {
  options = options || {};
  const request = new XMLHttpRequest();
  const task = new Promise((resolve, reject) => {
   ...
  });
  task.abort = () => request.abort() // lol
  return task
}

@nfantone
Copy link

Any movements on this? With all major green browsers supporting it, AbortController is steadily becoming the norm with many sources and docs out there showing people how to use it. Would be nice to have it baked into unfetch.

@mikestopcontinues
Copy link

node-fetch is actively recommending abort-controller. At least one of the PRs will bring support. What's preventing movement on this? What's everyone using in the meantime?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
8 participants