简体   繁体   中英

How to make multiple http calls from the nodejs server with a common callback?

I want to make multiple http calls in nodejs sever with common callback. Is there any modules available for doing it?

You can use async library or underscore.

I often use underscore for this. Assuming you are making n http calls

var http_done = _.after(n, function() {
   // final callback
});

for... {
  ajax_call(..., function(response) {
     // Do something with response
     http_done();
  });
}

I have used this Async NPM to solve this. https://www.npmjs.com/package/async

async.parallel([
        function(callback){
             callback(null,1)
        },
        function(callback){
             callback(null,2)
        },      
    ],
    function(err, results){

       console.log(results); //Output [1,2]

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM