[whatwg/dom] Proposal: AbortSignal.wrap (Issue #1147)

While we are starting to see an expansion of Promise-based APIs that directly support cancelation using an AbortSignal, there are still a good number out there that do not. One common pattern for these, using async/await syntax is to check if the AbortSignal has been triggered after the await, before performing other actions, e.g.

```js
async function doSomething(options) {
  const { signal } = options;
  await someAsyncTaskThatDoesNotSupportCancelation();
  if (signal.aborted) throw signal.reason;
  // carry on my wayward son...
}
```

The equivalent for Promise then syntax would be:

```js
someAsyncTaskThatDoesNotSupportCancelation().then(() => {
  if (signal.aborted) throw signal.reason;
  // carry on...
});
```

The pattern is straightforward enough and not that complicated but I'm wondering if a simple `AbortSignal.wrap()` utility would be worthwhile to reduce boilerplate:

```js
const wrapped = signal.wrap(() => {
  // carry on...
});
// calling wrapped will throw signal.reason if called after signal is triggered.

someAsyncTaskThatDoesNotSupportCancelation().then(wrapped);
```

I certainly wouldn't consider it a Must Have but a useful convenience?

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

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

Received on Wednesday, 25 January 2023 15:07:59 UTC