I am creating an app in NodeJS just to send JSON documents to a distant API.
My problem is that my JSON is big, and Node tries to send as fast as possible items from the array, which is way too fast for the API.
How could I slow down a little bit the forEach function ? I really can't find where to put a setTimeout somewhere, Node is always going on with the next step...
I show you my code, I'm pretty sure is not that hard, but it feels like this exactly what NodeJs is not really made for, is it ?
var datajson = "MY JSON";
datajson.forEach(function (element) {
var options = {
method: 'POST',
url: 'http://XXX',
headers: { XXX },
body: { XXX },
json: true
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
});