Async + Axios - Rate control

Viewed 3256

I am currently sending a GET request to an API using promises 200 times. Not surprisingly, there is a max number of connections allowed within such a short time span.

I am using axios to do this:

const memberPromises = members.map(member => axios.get(`players/%23${member.tag}`))

axios.all().then().catch() // etc

..where members can be up to 200 elements.

There doesn't seem a way to rate-control these requests natively within axios, but how would I use async's (or another library if there is better) queue method with a concurrent parameter to limit the number of simultaneous requests?

2 Answers
const axios = require('axios');
const axiosThrottle = require('axios-throttle'); 
//pass axios object and value of the delay between requests in ms
axiosThrottle.init(axios,200)
const options = {
  method: 'GET',
};
const promises = [];
const responseInterceptor = response => {
  console.log(response.data);
  return response;
};

//add interceptor to work with each response seperately when it is resolved
axios.interceptors.response.use(responseInterceptor, error => {
  return Promise.reject(error);
});

for (let index = 0; index < members.length; index++) {
  options.url = `http://yourapiurl/players/%23${member.tag}`;
  promises.push(axiosThrottle.getRequestPromise(options, index));
}

//run when all promises are resolved
axios.all(promises).then(responses => {
  console.log(responses.length);
});

https://github.com/arekgotfryd/axios-throttle

Related