0

On the script below I need to add a wait every time fetch runs cause if i send them too fast pipedrive will not update properly. I tried setTimeout but it didnt work... any ideas?

var response = [{
  id: 118,
  deal_id: 329,
  item_price: 53.37,
  quantity: 6
}, {
  id: 119,
  deal_id: 329,
  item_price: 69.07,
  quantity: 6
}];

var txtStr = '';
var myurl = '';
var mybody = '';

for (var i = 0; i < response.length; i++) {
  txtStr = txtStr + response[i].id;
  myurl = "https://api.pipedrive.com/v1/deals/" + response[i].deal_id + "/products/" + response[i].id + "?api_token=123456";
  mybody = JSON.stringify({
    'id': response[i].id,
    'deal_product_id': response[i].deal_id,
    'item_price': response[i].item_price,
    'quantity': response[i].quantity,
  })

  fetch(myurl, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json'
      },
      body: mybody
    })
    .then(function(response) {
      var produto_id = response.id;
      return response.text();
    }).then(function(body) {
      var output = {
        response: body
      };
      callback(null, {
        id: 123,
        rawHTML: body
      });
    }).catch(function(error) {
      callback(error);
    })
}
Deca
  • 1
  • 2

1 Answers1

0

Since this is async, put your fetches in a que, then on your callback, check the array to see if there is another fetch waiting.

Bindrid
  • 3,655
  • 2
  • 17
  • 18
  • Thank you @Bindrid , I will try this option, but I am very novice will take a while to figure it out! – Deca Dec 12 '15 at 15:20