mo / abortcontroller-polyfill

Polyfill for the AbortController DOM API and abortable fetch (stub that calls catch, doesn't actually abort request).
MIT License
328 stars 26 forks source link

Abort event handler should execute immediately, not debounced #36

Closed lukasolson closed 3 years ago

lukasolson commented 5 years ago

I've got the following snippet of code:

const controller = new AbortController();
controller.signal.addEventListener('abort', () => console.log('aborting'));
(function () {
  controller.abort();
  console.log('done aborting');
})();

When I run this in Chrome, I get the following output:

aborting
done aborting

But when I run this using this polyfill, I get the following:

done aborting
aborting

It looks like this is because the events handlers are debounced. If I were to use signal.onAbort = ... it would behave the same as Chrome, but I'd rather use the addEventListener handler as it avoids directly mutating signal.

lukasolson commented 4 years ago

@mo Do you agree with this assessment? I can throw together a PR for this as well.

mo commented 4 years ago

@lukasolson yup, it's a bug in the polyfill. A PR for this would be great.

benlesh commented 4 years ago

I came here to say this. For my use case, this is unusable because it's inaccurate. The important part of the abort signal is that is synchronously tears down whatever it is supposed to tear down.

benlesh commented 4 years ago

So FWIW: I started trying to build a PR for this repository, but it seemed like I was rewriting most of the implementation here, it didn't seem like this repo was actively maintained, and I had need of an AbortSignal that properly dispatched events, so I made a new package here:

https://www.npmjs.com/package/yet-another-abortcontroller-polyfill

I hope this helps someone else that finds this stale issue.

mo commented 3 years ago

fixed by ambar in the latest version