My Node application sends invoice data to an API and I would like my application to send the data in series / wait a while after each invoice post, because the API server is blocking the requests if there are over 20 in a second.
I think one way would be making the for-loop pause after each iteration, but I am not sure how to achieve it.
Below is the code where I currently push the "invoice generation" -promises to an array in a for-loop and then resolve them with Promise.all.
All comments appreciated.
// First, get access token from the service API
serviceAPI.auth().then((token) => {
// Array for the invoice promises
let invoicePromises = [];
// Push promises to the array
for (let i = 0; i < rowAmount; i++) {
invoicePromises.push(serviceAPI.generate(i, sheetData[i], token));
}
return Promise.all(invoicePromises);
}).then((results) => {
console.log(results);
return utils.sortReport(results); // Return sorted results
}).catch((e) => { console.error(e)});