I am looping in an array of products then for each product, I retrieve data from an API (I don't have control on it). Once data is collected for all products, I save the data in a file (that will then be imported later on).
let dataToStore = [];
let requests = products.map((product) => {
axios('https://localhost:4000/getProduct/' + product.id).then((data) => {
dataToStore.push({ product: data.response });
}, 1000);
});
Promise.all(requests)
.then(() => {
saveDataInFile(dataToStore, filename);
})
.catch(function (err) {
console.error("Promise.all error", err);
});
My concern here is sometimes, there can be 1000+ products, which means 1000 requests, and get sometimes address not found and I could potentially slow down the website.
I would like to set a timeout between request, but have no idea how to do so with Promises here.
I tried to wrap the axios request in a setTimeout but then saveDataInFile gets called before everything is done.