Unless you extend the language:

  var results = cond ? async1(_) : async2(_);

There is no free lunch: either you are ready to go with a language 
extension and you'll be able to reduce the amount of extra code/indentation 
drastically and get the same readability/maintainability as with good old 
sync JavaScript, or you stick to JavaScript and you'll have to live with 
the problem. 

You may get some relief from async libraries but you will still have one 
"function" keyword and an indented function body somewhere in your code 
every time you need to call an async call to get a value. If you have thin 
layers of logic, this is viable. If you have thick layers with lots of 
loops and conditionals, I find it just overwhelming.

Bruno


On Wednesday, August 8, 2012 6:00:43 PM UTC+2, Dan Milon wrote:
>
> Indeed, you cannot get anything less than +1 indentation. 
>
> Thanks for your input! 
> danmilon. 
>
> On 08/08/2012 04:54 PM, Tim Caswell wrote: 
> > On Wed, Aug 8, 2012 at 4:13 AM, Dan Milon <[email protected]<javascript:>> 
> wrote: 
> >> That will work, indeed, but it annoys me that indentation and code 
> >> readability gets fucked up. You have to follow code traces in order to 
> >> understand the ordering of execution. 
> > Dan, you're not going to get any less indentation than my simple 
> > example.  There has to be at least one level of indentation after your 
> > callback no matter what unless you extend the language. 
> > 
> > If it makes you feel better you can indent your conditional in one 
> > level as well so that they are both one level deep. 
> > 
> > What I was showing is that function statements hoist their value, so 
> > you can call them before the line that defines them is executed.  This 
> > means your program can execute in a top-down linear fashion without 
> > ever nesting any more than one level deep for callbacks. 
> > 
> > As far as what the async library provides, I have no idea, I've never 
> > used it.      It will require at least one function block with a level 
> > of indentation though. 
> > 
> > I find using named functions and other clever uses of the language 
> > itself provides just as clear code most the time and with a lot less 
> > abstraction. 
> > 
> >> I already use async for other cases. Do you find any of its control 
> flow 
> >> functions helpful for the if/else problem? 
> >> 
> >> Thanks, 
> >> danmilon. 
> >> 
> >> 
> >> 
> >> On 08/08/2012 05:56 AM, Tim Caswell wrote: 
> >>> If there really are only two functions that have the same callback 
> >>> signature, then it's super easy taking advantage of named function 
> >>> value hoisting. 
> >>> 
> >>>       if (cond) async1(onDone); 
> >>>       else async2(onDone); 
> >>> 
> >>>       function onDone(err, result) { 
> >>>         // the function finished 
> >>>       } 
> >>> 
> >>> But I suspect the question involves more complicated cases in 
> practice. 
> >>> 
> >>> On Tue, Aug 7, 2012 at 9:44 PM, Andy <[email protected] <javascript:>> 
> wrote: 
> >>>> I would personally go with using promises. 
> >>>> 
> >>>> var q = require('q'); 
> >>>> q.ncall(function() { 
> >>>>      if(cond) { 
> >>>>          return async1(); // this is a promise 
> >>>>      } 
> >>>>      return async2(); // so is this 
> >>>> }).then(function(res) { 
> >>>>      // hooray! one of them finished 
> >>>> }).error(function(res) { 
> >>>>       // something went wrong! 
> >>>> }); 
> >>>> 
> >>>> -- 
> >>>> 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]<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