Harmony is bringing generators. Generators make it possible to write async 
code almost like sync code. For example if/else could be written as

  if (cond) yield async1();
  else yield async2();
  // continuation code goes here

or equivalently as

  cond ? yield async1() : yield async2();
  // continuation here

Yield will play nicely with all the JavaScript constructs (try/finally 
requires a special hack, though). For example you'll be able to write:

binary operators: yield async1() <= yield async2()
call chaining: yield (yield async1()).async2()

You'll only need two helper functions: 
* an invokeAsync helper to invoke callback-style APIs from yield-style 
functions
* a runAsync helper to invoke a yield-style function from standard 
callback-style node code.

My only problem with this is that it may be costly with multi-level call 
stacks (asyncF1 calling asyncF2 calling asyncF3 ...) because you'll get one 
generator per async stack frame. This won't be as efficient as CPS 
transforms or fibers, unless the cost of a generator is really low.

More info on this on: 
http://bjouhier.wordpress.com/2012/05/18/asynchronous-javascript-with-generators-an-experiment/

Bruno



On Thursday, August 9, 2012 2:53:26 AM UTC+2, Dan Milon wrote:
>
> Thats your post that i was referring to ;) I liked the CPS style. 
>
> Do you know if harmony is bringing any goodness for these situations? 
>
> On 08/08/2012 08:18 PM, José F. Romaniello wrote: 
> > I wrote a blogpost about this specific topic: 
> > 
> > http://joseoncode.com/2012/06/24/messing-with-cps-in-js/ 
> > 
> > for me there are two ideal situations: 
> > 
> >   * a language with an specific syntax for asynchronous flows like 
> >     streamlinejs, icedcoffeescript, f# 
> >   * or make a Continuation Passing Style version of the  javascript 
> >     constructs (if, for, while, try/catch) (similar to lisp family 
> >     languages) 
> > 
> > there are also a lot of async libraries I know. 
> > 
> > 2012/8/7 Dan Milon <[email protected] <javascript:> <mailto:
> [email protected] <javascript:>>> 
> > 
> >     I am wondering which are the different patterns to handle cases like 
> > 
> > 
> >     var results 
> >     if (cond) { 
> >       async1(function (err, res) { 
> >         results = res 
> >       }) 
> >     } 
> >     else { 
> >       async2(function (err, res) { 
> >         results = res 
> >       }) 
> >     } 
> >     // here need to do something with results. 
> > 
> >     The problem is obvious, but i cannot see any good way to overcome 
> it. 
> > 
> >     -- 
> >     Job Board: http://jobs.nodejs.org/ 
> >     Posting guidelines: 
> >     https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines 
> >     You received this message because you are subscribed to the Google 
> >     Groups "nodejs" group. 
> >     To post to this group, send email to 
> > [email protected]<javascript:> 
> >     <mailto:[email protected] <javascript:>> 
> >     To unsubscribe from this group, send email to 
> >     [email protected] <javascript:> 
> >     <mailto:nodejs%[email protected] <javascript:>> 
> >     For more options, visit this group at 
> >     http://groups.google.com/group/nodejs?hl=en?hl=en 
> > 
> > 
> > -- 
> > Job Board: http://jobs.nodejs.org/ 
> > Posting guidelines: 
> > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines 
> > You received this message because you are subscribed to the Google 
> > Groups "nodejs" group. 
> > To post to this group, send email to [email protected]<javascript:> 
> > To unsubscribe from this group, send email to 
> > [email protected] <javascript:> 
> > For more options, visit this group at 
> > http://groups.google.com/group/nodejs?hl=en?hl=en 
>
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to