Async parallel requests are running sequentially

前端 未结 3 1712
渐次进展
渐次进展 2020-12-29 00:27

I am running a server using Node.js and need to request data from another server that I am running (localhost:3001). I need to make many requests (~200) to the

3条回答
  •  臣服心动
    2020-12-29 00:59

    Sounds like you're just trying to download a bunch of URLs in parallel. This will do that:

    var request = require('request');
    var async = require('async');
    
    var urls = ['http://microsoft.com', 'http://yahoo.com', 'http://google.com', 'http://amazon.com'];
    
    var loaders = urls.map( function(url) {
      return function(callback) {
            request(url, callback);
      }
    });
    
    async.parallel(loaders, function(err, results) {
            if (err) throw(err); // ... handle appropriately
            // results will be an array of the results, in 
            // the same order as 'urls', even thought the operation
            // was done in parallel
            console.log(results.length); // == urls.length
    });
    

    or even simpler, using async.map:

    var request = require('request');
    var async = require('async');
    
    var urls = ['http://microsoft.com', 'http://yahoo.com', 'http://google.com', 'http://amazon.com'];
    
    async.map(urls, request, function(err, results) {
            if (err) throw(err);          // handle error 
            console.log(results.length);  // == urls.length
    });
    

提交回复
热议问题