The parallel function in Async should make this pretty easy

var async = require('async');

async.parallel([
  function(callback){
    Object1.retrieveNum1Async(function(err, num1){
      callback(err, num1);
    });
  },

  function(callback){
    Object2.retrieveNum2Async(function(err, num2){
      callback(err, num2);
    });
  },

  function(callback){
    Object3.retrieveNum3Async(function(err, num3){
      callback(err, num3);
    });
  }
], function(err, results){
  if(err) throw err;

  // Your results will now be an array of [num1, num2, num3]
  var temp = (results[0] + results[1]) / results[2];
);


Don't forget to add an Error as the first parameter to the callback in your 
"retreiveAsync" functions.

On Monday, April 22, 2013 2:54:50 PM UTC-5, Slobodan Blazeski 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 [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

--- 
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to