For each id in an array of ids, I want to make an API request to an endpoint somapi.com/todo/{id}. Let's say the API is rate limited to 25 requests per second and a request takes on average 1 second. We'll assume 100000 ids. My first implementation would be something like:
const ids = [...Array(100000).keys()];
const downloadTodos = async (ids) => {
for (const id of ids) {
await downloadTodo(id)
}
}
But that would be slow and far from utilizing the max rate limit. How can I make downloadTodos faster by using parallelization/async-await? Maybe by awaiting N requests in paralell at any given time?
Promise.all()/allSettled()or any of the many promise queue implementations you can find on NPM.