faizanjehangir
faizanjehangir

Reputation: 2851

request api in foreach

I have an array of data, against which I query an api using request. With a callback executing after each response for the request that is made. However, in doing so, I end up launching parallel requests for all the items in the array. Here is what I am doing:

exports.getData = function(arr, cb){
    arr.forEach(function(data){
        var query = {
            //some data here
        };
       request({
           url: 'http://x/y',
           json: query,
           method: 'POST',
           headers: {
               'Content-Type': 'application/json'
           }
       }, function(error, res, body){
           if (error){
               console.log(error);
           } else{
               cb(res.body);
           }
       });
    });
};

I want to setTimeOut of x seconds in the above code. Should I implement a naive delay after each request? Or something else?

Upvotes: 0

Views: 2420

Answers (2)

Ikrum Hossain
Ikrum Hossain

Reputation: 210

Updated : parallel request to series of request.

You should use series of request. use async module. see below

exports.getData = function(arr, cb){

  // make an array of function
  var funcArray = [];

  arr.forEach(function(data){
      var query = {
          //some data here
      };

     funcArray.push(function(callback){    
        request({
           url: 'http://x/y',
           json: query,
           method: 'POST',
           headers: {
               'Content-Type': 'application/json'
           }
        }, function(error, res, body){
           // 5 second interval for each request
           setTimeout(function(){ callback(error, body); }, 5000);
        });
      });
  });


  // now run all tasks on series
  async.series(funcArray,function(err, result){

      // now you will get result for all request

      // handle error
      // do what ever you want with result
  });
}

Upvotes: 1

Sachin
Sachin

Reputation: 1218

forEach loop will ideally end in no time, so you can first add all query parameters in queryPool and then execute them one by one.

You can refer to this code.

exports.getData = function(arr, cb){
    var queryPool = [];
    function execQuery(){
       if(queryPool.length == 0) return
       var query = queryPool.slice(0,1)
       queryPool = queryPool.slice(1,queryPool.length)
       request({
           url: 'http://x/y',
           json: query,
           method: 'POST',
           headers: {
               'Content-Type': 'application/json'
           }
       }, function(error, res, body){
           if (error){
               console.log(error);
           } else{
               cb(res.body);
           }
           execQuery();
       });
    }
    arr.forEach(function(data){
        var query = {
            //some data here
        };
       queryPool.push(query);
    });
    execQuery();
};

This code assumes function given in request executes in all conditions, failure or success.

Upvotes: 0

Related Questions