Re: [whatwg/dom] Encourage use of AbortError for cancellation? (#927)

> Worth mentioning that for Node it would be more convenient if the method Domenic is proposing was on `AbortController` or `AbortSignal` and not on `DOMException` since we can't guarantee internal methods will always throw `DOMException`s and DOMException isn't exposed as a global.

One option would be to add a global `AbortError` constructor, which delegates to whatever a given host thinks is appropriate. For example in browsers it could just be:

```js
function AbortError(message="Aborted") {
    throw new DOMException(message, "AbortError");
}
```

In node it might vend something different (but still with `.name === 'AbortError'`).

While @jakearchibald's suggestion is great for checkpoints, it doesn't quite work for cases where you need to produce an `AbortError` asynchronously e.g.:

```js
function animationFrame(abortSignal) {
    return new Promise((resolve, reject) => {
        abortSignal?.assertNotAborted(); // Checkpoint use case as suggested in OP
        const frameRequest = requestAnimationFrame(time => resolve(time));
        abortSignal?.addEventListener("abort", () => {
            cancelAnimationFrame(frameRequest);
            reject(new AbortError()); // Case I'm suggesting, this isn't covered by OP
        }, { once: true });
    });
}
```

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

Received on Wednesday, 3 February 2021 06:26:31 UTC