- From: Nicholas C. Zakas <notifications@github.com>
- Date: Tue, 09 Apr 2024 14:27:13 -0700
- To: whatwg/dom <dom@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/dom/issues/1277@github.com>
### 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