Friday 21 April 2017

NodeJs, Promises not waiting

I have this function that should make 300 requests for a web page (for benchmarking), however the Promise.all is not waiting for those requests to finish before outputting an empty array, any ideas?

function requestLoop(){

  var resultSet= [];

  // options.requests = 300
  // options.url = http://localhost/

  for(var c=1;c<=options.requests; c++){

    http.get(options.url, function(res){

    //  resultSet.push( { request: c, statusCode: res.statusCode});

      resultSet.push(new Promise(function(res){ return { request: c, statusCode: res.statusCode}; }));

    });

  }

  Promise.all(resultSet).then(function(){
    console.log(resultSet);
  });

  return;

}

Promise is bluebird and http is the normal http package



via B1scuit

No comments:

Post a Comment