Re: [whatwg/fetch] Proposal: fetch with multiple AbortSignals (#905)

Great comment from @jakearchibald.

Some proposed changes -
1. Propagate reason from the aborted signal to the wrapper signal;
2. Don't attempt unsubscribe if subscription hasn't been done yet;
3. Typescript added.

```typescript
function anySignal(signals: AbortSignal[]) {
    const controller = new AbortController();
    const unsubscribe: (() => void)[] = [];

    function onAbort(signal: AbortSignal) {
        controller.abort(signal.reason);
        unsubscribe.forEach((f) => f());
    }

    for (const signal of signals) {
        if (signal.aborted) {
            onAbort(signal);
            break;
        }
        const handler = onAbort.bind(undefined, signal);
        signal.addEventListener('abort', handler);
        unsubscribe.push(() => signal.removeEventListener('abort', handler));
    }

    return controller.signal;
}
```

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

Message ID: <whatwg/fetch/issues/905/1425133682@github.com>

Received on Friday, 10 February 2023 03:46:02 UTC