Retrieving generator references

Axel Rauschmayer axel at
Sat Nov 22 20:18:35 PST 2014

>> * The creation of the generator object. This is where the generator function is like a constructor and where you’d expect `this` to refer to the generator object.
> Nope, not a constructor (and if you don't call with 'new', what is 'this', pray tell?).

I only mean it is “like a constructor” in that it is a function that, when invoked (in some way), returns a fresh object.

The main purpose of a generator function is to specify the behavior of the generator object. But it also serves as its constructing entity. A double duty.

>> * The behavior. This is where the generator function is like a real function.
>> A result of this mixing of concerns is that using `next()` to start a generator feels slightly off and that the argument of that first `next()` invocation is completely ignored.
> We've discussed a special generator head form to declare that name, but not for ES6. For now it's Pythonic, except we decided not to throw if the initial .next call passes a non-undefined value.
>> Alas, I have no idea how to disentangle these concerns, but it would be nice if we were able to.
> We're not changing generators at this point, but Nicholas's request for a way to reference the running generator-iterator instance is worth discussing more. It can't and shouldn't be 'this'. Does it violate POLA to provide (via another special form) for all generators? Users can provide their own bindings as noted, so is it a big problem?

I’d pass the generator object reference to the generator function via `next()`, as the first step after creating the generator object. Not pretty, but relatively clean.

> I think the task.js reference, and ES7 async/await, point to a better direction: use helpers (libraries now, syntax soon) to automate harder and avoid the need for the "me" reference.

I agree, I can’t think of a solution that wouldn’t be much too complicated for the minor problem that it solves.

Dr. Axel Rauschmayer
axel at

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list