Async parallel requests are running sequentially

前端 未结 3 1686
渐次进展
渐次进展 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:48

    Try this:

    var async = require("async");
    var request = require("request");
    var show_file = function (file_number,cb) {
        //..Sync ops
         var file_index = Math.round(Math.random() * 495) + 1;
         var pinnacle_file_index = 'http://localhost:3001/generate?file='+file_index.toString();
        //request instance from Request npm Module
        //..Async op --> this should make async.each asynchronous
        request(pinnacle_file_index, function (error, response, body) {
           if(error)
               return cb(error);
           var object_key = "file_" + file_number.toString();
          pinnacle_data[object_key] = JSON.parse(body);
          return cb();
        });
    };
    
    async.each(
      lookup_list, 
      show_file,
      function (err) {
        if(err){
           console.log("Error",err);
        }else{
           console.log("Its ok");
           console.log(pinnacle_data);
       }
    });
    

提交回复
热议问题