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

Creating an already aborted AbortController #959

Closed
jasnell opened this issue Mar 9, 2021 · 5 comments · Fixed by #960
Closed

Creating an already aborted AbortController #959

jasnell opened this issue Mar 9, 2021 · 5 comments · Fixed by #960
Labels
topic: aborting AbortController and AbortSignal

Comments

@jasnell
Copy link
Contributor

jasnell commented Mar 9, 2021

In Node.js, we have had a couple of use cases pop up recently with the need to mark an AbortController already aborted immediately after creation. Typically, this would be done as: const ac = new AbortController(); ac.abort(); return ac;.

It would be helpful to have a utility that makes this easier...

Either:

const ac = new AbortController({ aborted: true });

or

const ac = AbortController.aborted();

The abort event would never trigger on such objects, which is perfectly fine. We already tell users to check the aborted property before attaching abort listeners.

If the idea seems reasonable, I can work up a PR with the change. I would just need to know which approach (init options vs. static factory function) is preferred.

@annevk annevk added the topic: aborting AbortController and AbortSignal label Mar 9, 2021
@annevk
Copy link
Member

annevk commented Mar 9, 2021

Do you need an AbortController or an AbortSignal? AbortSignal.aborted() would make more sense to me.

cc @jakearchibald @domenic

@jasnell
Copy link
Contributor Author

jasnell commented Mar 9, 2021

Good question... I think either would work for the immediate case to be honest.

@domenic
Copy link
Member

domenic commented Mar 9, 2021

I like AbortSignal.aborted(). I agree that the controller seems pretty useless if you've already aborted.

@domenic
Copy link
Member

domenic commented Mar 9, 2021

It'd be good to describe your use cases here, especially if you can expand them to be environment-agnostic so that people understand when they might also be applicable in web application code. That'd help make the pitch for multi-implementer interest.

@jasnell
Copy link
Contributor Author

jasnell commented Mar 9, 2021

The most recent use case I've seen this for is the easiest/clearest... It's in the context of an HTTP request in Node.js:

  1. An AbortController/AbortSignal is associated with an HTTP request.
  2. The AbortController/AbortSignal is lazily created (that is, it is only created the first time it is explicitly asked for)
  3. The HTTP request may already be aborted by the time the AbortController/AbortSignal is requested, in which case we want to just return an already aborted signal as opposed to undefined or throwing.

Very simple, and along the same lines as (for instance) Promise.reject().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: aborting AbortController and AbortSignal
Development

Successfully merging a pull request may close this issue.

3 participants