Re: [whatwg/streams] `.throw()`ing an async iterator (Issue #1284)

`for..of` doesn't call `throw()` either.

```javascript
let it = {
    [Symbol.iterator]: () => it,
    next() {
        return { done: false, value: "a" };
    },
    throw(e) {
        console.log("it.throw() called");
        return { done: true };
    },
    return() {
        console.log("it.return() called");
        return { done: true };
    }
};
for (const elem of it) {
    throw new Error("boom!");
}
```
The above snippet logs:
```
it.return() called
Uncaught Error: boom!
```
So even if we added such a hook, it wouldn't do anything in your example. AFAIK the only built-in construct that interacts with `throw()` is `yield*`.

I don't see how we could ever make `throw()` work. With generators, `throw()` can still cause the generator to *resume normally*, as in the following example:
```javascript
function* gen() {
  while (true) {
    try {
      yield;
    } catch {
      continue;
    }
  }
}
```
But with `for..of`, throwing inside the loop body *must* propagate upwards. We cannot "resume iteration" afterwards. So we only have `return()`.

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

Message ID: <whatwg/streams/issues/1284/1598684724@github.com>

Received on Tuesday, 20 June 2023 12:33:00 UTC