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

Brendan Eich brendan at mozilla.com
Thu Jan 23 16:43:01 PST 2014


Bradley Meck wrote:
> Is that true even though then should fire after the generator unwinds 
> its stack?

True if .then always runs in a later turn -- sorry.

Still, my point stands: you are not using task.js-like 
scheduler/combinator approach for an apples-to-apples comparison with 
await. If you do, then we're down to the obligation of a task.js 
download, and some syntactic sugar.

// assume req in scope
spawn(function*()  {
   try  {
     var  session  =  yieldlogin(req);  // wait until the promise resolves
     gotoProfile(session);
   }
   catch  (e)  {
     printError(e);
     gotoLogin();
   }
   // implicit return undefined
});

BTW, in ES7, no "use await"; will be needed ;-).

If you want to control when the task starts, you'd use new Task. If you 
want to send req, you'd control starting via t = new 
Task(function*(){...}) and call t.next(req). Hope this is all clear from 
http://taskjs.org/ and the code.

> I am using regenerator right now while playing with this stuff.

Cool -- can you try task.js?

/be


More information about the es-discuss mailing list