I am building a React app that fetches and aggregates data from API calls and calculates/shows some stats.
I am using axios to make the API calls in
export const getData = async (name) => {
const element_list = await structuredRequest(name)
const aggregated_list = await Promise.all(
element_list.map(async (element) => {
element.size = await getSize(element.number)
return element
})
)
return aggregated_list
}
where structuredRequest
returns an array containing all the the elements for which I want to collect their size. This is done by calling:
async function getSize(number) {
let url = `https://api.foobar.com/${number}`
const result = await axios.get(url)
return result.data.size
}
This approach works well when the number of elements, in other word the length of element_list
, is small. However, for larger requests I start running into err_insufficient_resources as the browser cannot process the >1k requests at once that result from getData
.
Is there a way to "throttle down" the rate of concurrent requests, or to have axios automatically manage it?
Thanks a lot!