Skip to content

Proposal: AbortSignal.none() #1277

Open
@nzakas

Description

@nzakas

What problem are you trying to solve?

When writing a function that accepts an AbortSignal, I typically write a function that looks like this:

async function doSomething(arg1, arg2, { signal } = {}) {
   // body
}

Because signal is optional, I'd like a way to specify a default value that can be used throughout the function to indicate "this function is not aborted"

What solutions exist today?

There are two solutions I've used so far, and both have proven error prone:

async function doSomething(arg1, arg2, { signal } = {}) {
   // solution 1: check for signal constantly
   if (signal) {
     signal.throwIfAborted();
   }

   // solution 2: optional chaining
  signal?.throwIfAborted()
}

In both cases, the issue I've faced is that it's easy to forget to check if signal has been passed and so errors tend to show up later.

How would you solve it?

I'd create a new AbortSignal.none() method that would return an AbortSignal that is not aborted and never will be. That way, I could write something like this:

async function doSomething(arg1, arg2, { signal = AbortSignal.none() } = {}) {
   
    // no need to check anything
    signal.throwIfAborted();
}

Anything else?

Thank you for reading. 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    addition/proposalNew features or enhancementsneeds implementer interestMoving the issue forward requires implementers to express interesttopic: abortingAbortController and AbortSignal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions