Re: [whatwg/streams] Cancelling and exception handling async iteration of ReadableStreams (Issue #1255)

@domenic it's basically unsafe to use async iteration on anything that acquires resources, unless you do something like wrap it with `abortableAsyncIterator`:

```ts
const abortable = <T,>(promise: Promise<T>): Promise<T> =>
  Promise.race([promise, this.abortedPromise as Promise<T>])

const abortableAsyncIterable = <T,>(
  iter: AsyncIterable<T>
): AsyncIterable<T> => ({
  [Symbol.asyncIterator]: () => {
    const inner = iter[Symbol.asyncIterator]()
    return {
      next: () => abortable(inner.next()),
      return: inner.return,
      throw: inner.throw,
    }
  },
})
```

I think this should be a builtin function or even special syntax like
```ts
for await (const elem of asyncIterable until signal) {
  ...
}
```

Because it's easy to forget that we'll leak resources without doing this, and I'm sure a lot of JS developers don't even realize it.

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

Message ID: <whatwg/streams/issues/1255/2442964298@github.com>

Received on Tuesday, 29 October 2024 01:12:14 UTC