0

I have variable number of async function to run. For two function, I can write like this:

async.parallel([
      function(parallelCb) {
        client.get('statuses/user_timeline', {screen_name: 'rajeevnodetest'}, function(error, tweets, response){
          parallelCb(null, {error: error, tweets: tweets});
        });    
    },
      function(parallelCb) {
        client.get('statuses/user_timeline', {screen_name: 'rajeev_jayaswal'}, function(error, tweets, response){
          parallelCb(null, {error: error, tweets: tweets});
        });    
    },
  ],  function(error, results) {
        // results will have the results of all 2
        res.send(JSON.stringify(results));
    });

How should I call variable number of function inside async.parallel ? I tried adding for loop inside async.parallel but it did not work. Any suggestion ?

mscdex
  • 104,356
  • 15
  • 192
  • 153
Rajeev Jayaswal
  • 1,423
  • 1
  • 20
  • 22

3 Answers3

1

You can use async.times function Like this

function(parallelCb) {
    async.times(50, function (n, next) {
        client.get('statuses/user_timeline', {screen_name:'rajeevnodetest'}, 
            function(error, tweets, response){
                next(err, tweets);
           });   
    }, function (error, results) {
      // do something with your results
          parallelCb(null, {error: error, tweets: results});
    }
}

So if you want to Run n number of async function before calling another method You will have to use async.series method using async.times in it because according to async.parallel documentation it Run the tasks collection of functions in parallel, without waiting until the previous function has completed.

Ahmed Hassan
  • 567
  • 3
  • 10
1

I am assuming you will be reaching same api in order to retrive user tweets.

Therefore you have an array containing all tweeter handles:

var screenNames = ['rajeevnodetest', 'rajeev_jayaswal', ..., 'brandnewhandle'];

You can define a function to reach the API like this:

//Custom function to find user status list 
var findStatus = function(handle, callback) {
  client.get('statuses/user_timeline', {screen_name: handle}, function(error, tweets, response){
    callback(null, {error: error, tweets: tweets});
  });
}

Then you could use Asyn.map function in order to execute that function once for every element in your tweeter handle list like:

async.map(screenNames, findStatus, function(err, results) {
  // results will have the results of all 2
  res.send(JSON.stringify(results));
});

If you need to make calls to multiple API endpoints for each tweeter handle then you would need to use paralell and create an entry for each endpoint.

Cristian Colorado
  • 2,022
  • 3
  • 19
  • 24
0

you should look into EventEmitter

call the emit method withing your async functions and hook all the function you want to run to that event name

santi6291
  • 154
  • 2
  • 9