Sunday, 12 March 2017

Run n number of async function before calling another method in nodejs

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 ?



via Rajeev Jayaswal

No comments:

Post a Comment