Is there a way to throttle a for in
Javascript loop that has an async await api call inside of the loop? The problem here is that the API endpoint has a speed throttle of 1 second per request and some return with the error statusText: 'Too Many Requests'
, so the goal is to throttle the loop to hit the API every second (the order that the API returns the data is not of concern in this use case). Is there a way to do this with the current implementation or does throttling not work with this implementation?
const getProductSales = async () => {
const products = [...] // array of product slugs
const productSales = []
for (const product in products) {
try {
const response = await axios.get(
`https://www.someapi.com/api/v1/sales/${product}`
)
productSales.push(
{
product: product,
sales: response
}
)
} catch (err) {
console.error(err)
}
}
return productSales
}