Fwd: Reason why generators do not have references to themselves?

Brendan Eich brendan at mozilla.com
Sat Jan 25 10:50:30 PST 2014


[replying to a message you sent off-list, hope it is ok. /be]

Brendan Eich wrote:
> Bradley Meck wrote:
>> Perhaps, but I am still a bit concerned functionality wise that I do 
>> not have a clean way to force the `new generator()` piece of code to 
>> be inside of the generator.
>
> I think you've gone down a bad path. Even without task.js, couldn't 
> you put the promises .then'ing in the generator schedule,

Sorry, last word should be "scheduler" above.

The await syntax needs a scheduler behind the scenes, and task.js has 
one. The scheduler duck-types the result of yield expressions in the 
generator, and if it is a promise, arranges to .next the generator when 
the promise is fulfilled, or .throw at it when rejected.

Instead you compile await in a way that expands the promise .then'ing 
inline in the generator body, requiring a ref to the generator in a 
closure, for use from function expressions passed to .then and .catch.

Is this more clear?

/be

> instead of requiring each generator to open-code the .then/.catches, 
> which in turn requires you to have a ref to the generator? 


More information about the es-discuss mailing list