Why do generator expressions return generators?

Domenic Denicola domenic at domenicdenicola.com
Fri Sep 6 07:44:20 PDT 2013

Why can't they just return iterable-iterators?


const g = (for (x of [1, 2, 3]) x * x);

g.next();                   // returns { done: false, value: 1 }
g.next(5);                  // ???
g.throw(new Error("boo!")); // ???

As far as I can tell from the desugaring at http://wiki.ecmascript.org/doku.php?id=harmony:generator_expressions, the argument to `next` is completely ignored, and doing `throw` will bubble out so that `g.throw(new Error("boo!"))` is equivalent to `throw new Error("boo!")` except that the former will have some implementation stack frames at the top of the stack trace.

What does having these be generators buy us? I am almost sure I'm missing something.

More information about the es-discuss mailing list