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

AbortController ponyfill #94

Closed

Conversation

stereobooster
Copy link

@stereobooster stereobooster commented Sep 29, 2018

@@ -17,6 +17,9 @@ export default function(url, options) {

request.onerror = reject;

if (options.signal) options.signal.onabort = () => { request.abort(); }
request.onabort = () => reject(new DOMException('The user aborted a request.'));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is to simulate what Chrome does, but i don't insist on it

Copy link

@joaovieira joaovieira Oct 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per the spec (even that chrome exception has a name of AbortError):

reject(new DOMException('Aborted', 'AbortError'));

https://dom.spec.whatwg.org/#aborting-ongoing-activities
https://dom.spec.whatwg.org/#aborting-ongoing-activities-example
https://github.com/github/fetch/blob/master/fetch.js#L446

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder what browsers don't have it. I can't find data about this in caniuse. Shall we use Error instead?

@@ -0,0 +1,6 @@
export default function() {
this.signal = { onabort: () => {} };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -17,6 +17,9 @@ export default function(url, options) {

request.onerror = reject;

if (options.signal) options.signal.onabort = () => { request.abort(); }
request.onabort = () => reject(new DOMException('The user aborted a request.'));
Copy link

@joaovieira joaovieira Oct 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per the spec (even that chrome exception has a name of AbortError):

reject(new DOMException('Aborted', 'AbortError'));

https://dom.spec.whatwg.org/#aborting-ongoing-activities
https://dom.spec.whatwg.org/#aborting-ongoing-activities-example
https://github.com/github/fetch/blob/master/fetch.js#L446

@@ -17,6 +17,9 @@ export default function(url, options) {

request.onerror = reject;

if (options.signal) options.signal.onabort = () => { request.abort(); }
Copy link

@joaovieira joaovieira Oct 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stereobooster
Copy link
Author

Thanks for links to fetch spec.

I got the idea of passing aborted signal to the fetch, but I wonder what the use case? In what kind of API you would create N-signals and attach to fetch later? I create signal right before sending request and save it until I got response, to be able to cancel if I need to create another fetch (and only one allowed at the same time).

Should be trivial to implement, I just wonder do we need it or not

@joaovieira
Copy link

@stereobooster I also didn't have that use case yet. But I can think of using it as a hand-rolled circuit-breaking system where you use a signal to reject all requests going to a troubled/struggling downstream service.

Anyway, many people will use unfetch with isomorphic-unfetch so having consistent functionality with node-fetch (and thus, the spec) sounds right. Specially if it's easy :)

@stereobooster
Copy link
Author

Anyway, many people will use unfetch with isomorphic-unfetch so having consistent functionality with node-fetch (and thus, the spec) sounds right.

The point of unfetch it is minimal implementation, it is not fully compatible with spec and never meant to be. It is just subset of spec. The question is where to draw the line. I would say we need to support popular case only

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

Successfully merging this pull request may close these issues.

None yet

2 participants