I need to wait a second between iterations of an async.eachSeries call owing to the limitations of a third party API which protects against abuse by restricting the caller from making more than one request per second.
I'm not entirely clear on how to do this. Can I simply pass the series callback as the parameter function to a call to setTimeout? I suspect this won't work but thought I'd ask before I assemble all the code necessary to test it out. If setTimeout is not the answer, then what are my alternatives?
Yeah, setTimeout() can be used to add a delay where the callback() is normally called.
// vs. callback();
setTimeout(callback, 1000);
Any arguments that would be passed to the callback() can be passed after the timeout's delay.
// vs. callback(err);
setTimeout(callback, 1000, err);
// vs. callback(err, result);
setTimeout(callback, 1000, err, result);
Example:
async.eachSeries([1, 2, 3], function (n, callback) {
request3rdPartyAPI(n, function (err, result) {
setTimeout(callback, 1000, err);
});
});