Jose,

The continuation-passing-style patterns for conditionals and loops are 
relatively straightforwards but things get a lot hairier with try/catch and 
try/finally. But also, what about:

* unary and binary operators: async1(_) <= async2(_)
* lazy operators: async1(_) && async2(_)
* ternary operators: cond ? async1(_) : async2(_)
* async conditions in conditionals (ternary, if) and loops
* call chaining: async1(_).async2(_)
* composition: async1(_, async2(_))
* any combination of the above.

You could write generic continuation-passing-style helpers for all these 
language constructs but they would not help much with the code bloat and 
readability. And even if you get there, you still have to worry about lots 
of other details like trampolining calls so that you don't blow the stack 
with async calls that sometimes invoke their callback synchronously, etc. 
If you want to seamlessly and safely use all JS language constructs with 
async calls, you need a solid language extension (or the fibers library).

Actually, streamline.js started as an attempt to find CPS patterns for all 
the JS language constructs. The difficulty was to find "composable" 
patterns, i.e. patterns that can be combined with each other in arbitrary 
ways. Once I had the right patterns, it was relatively easy (although not 
completely trivial) to write a transformation algorithm that would 
automatically apply the patterns.

I described all the code patterns that I used (including try/catch and 
try/finally) in a tedious blog article: 
http://bjouhier.wordpress.com/2011/05/24/yield-resume-vs-asynchronous-callbacks/
 
Of course, you could apply these patterns "manually" without using the 
streamline compiler but this is clearly error prone and overwhelming.

Bruno

On Wednesday, August 8, 2012 7:18:56 PM UTC+2, 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 <danm...@gmail.com <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<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 nod...@googlegroups.com<javascript:>
>> To unsubscribe from this group, send email to
>> nodejs+un...@**googlegroups.com <javascript:>
>> For more options, visit this group at
>> http://groups.google.com/**group/nodejs?hl=en?hl=en<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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to