[whatwg/dom] Proposal: AbortSignal.none() (Issue #1277)

### What problem are you trying to solve?

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

```js
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:

```js
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:

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

### Anything else?

Thank you for reading. :smile:

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/1277
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/dom/issues/1277@github.com>

Received on Tuesday, 9 April 2024 21:27:17 UTC