Slobodan, You can use a counter for this:
function ifYouNeedToGetThreeNumbersAsynchronouslyAndThenDivideTheSumOfTheFirstTwoNumbersByTheThirdNumberAndPassTheResultOrErrorToTheCallbackThenCallThisFunctionBecauseItDoesThat(cb) { var num1, num2, num3; retrieveNumOne(function(er, n) { num1 = n then(er) }) retrieveNumTwo(function(er, n) { num2 = n then(er) }) retrieveNumThree(function(er, n) { num3 = n then(er) }) var i = 0, errState = null function then(er) { if (errState) return; if (er) return cb(errState = er) if (++i === 3) cb(null, (num1 + num2) / num3); } } Of course, yes, this is more lines of code, and a somewhat more verbose function name. But it's very explicit, and there's no question about what it's doing. "Call these functions and get the result, or error" is a pretty common pattern, so there are some libraries that abstract it away. Check out async. Personally, I prefer to not use control flow libraries like async. There's nothing wrong with them, per se, but I find that leaving that out requires me to have a lower tolerance for complexity in my programs, which is on the net a good thing. I definitely think that you should *write* a control flow library, even if you then throw it away, because there are few ways to get as familiar with these concepts than writing a library that implements them. On Mon, Apr 22, 2013 at 12:54 PM, Slobodan Blazeski <slobodan.blaze...@gmail.com> wrote: > Hi All > > I'm looking for suggestions of how to retrieve values asynchronously: > > In the synchronous world I have > > var num1 = Object1.retrieveNum1(); > var num2 = Object2.retrieveNum2(); > var num3 = Object3.retrieveNum3(); > > var result = (num1 + num2) / num3; > > > but since functions Object1.retrieveNum1,Object2.retrieveNum2 & > Object3.retrieveNum3 > retrieve data from the database or represent long calculation I need to pass > callbacks, > that leads me to below > > Object1.retrieveNum1Async(function(num1){ > Object2.retrieveNum2Async(function(num2){ > Object3.retrieveNum3Async(function(num3){ > var result = (num1 + num2) / num3; > }); > }); > }); > > is this the idiomatic way of doing this or there is something better > > > thanks > Bobi > > > > > -- > -- > 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 > > --- > You received this message because you are subscribed to the Google Groups > "nodejs" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to nodejs+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > -- -- 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 --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.