Async + Axios - 速率控制
Async + Axios - Rate control
我目前正在使用承诺向 API 发送 GET
请求 200 次。毫不奇怪,在这么短的时间内允许的最大连接数。
我正在使用 axios
来执行此操作:
const memberPromises = members.map(member => axios.get(`players/%23${member.tag}`))
axios.all().then().catch() // etc
..其中成员最多可以有 200 个元素。
似乎没有办法在 axios
内对这些请求进行本地速率控制,但我将如何使用 async
的(或其他库,如果有更好的)queue
方法用concurrent
参数来限制同时请求的数量?
这个怎么样?
将成员映射到承诺,超时后解决承诺
let RATE_LIMIT_BASE = 100; //100ms separation
let promises = members.map((member, idx) => {
return new Promise((resolve, reject) => {
setTimeout(
() =>
axios
.get(`players/%23${member.tag}`)
.then(res => resolve(res))
.catch(err => reject(err)),
RATE_LIMIT_BASE * idx
);
});
});
Promise.all(promises).then().catch(); // etc;
在评论中,正如marvel308所说,
this could fail in certain cases, since setTimeout just guarantees
that the code will execute atleast 100ms after setTimeout is called,
they could still queue up in the event queue in case some CPU
intensive task was blocking the call stack
感谢 marvel308 的建议
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);
});
我目前正在使用承诺向 API 发送 GET
请求 200 次。毫不奇怪,在这么短的时间内允许的最大连接数。
我正在使用 axios
来执行此操作:
const memberPromises = members.map(member => axios.get(`players/%23${member.tag}`))
axios.all().then().catch() // etc
..其中成员最多可以有 200 个元素。
似乎没有办法在 axios
内对这些请求进行本地速率控制,但我将如何使用 async
的(或其他库,如果有更好的)queue
方法用concurrent
参数来限制同时请求的数量?
这个怎么样?
将成员映射到承诺,超时后解决承诺
let RATE_LIMIT_BASE = 100; //100ms separation
let promises = members.map((member, idx) => {
return new Promise((resolve, reject) => {
setTimeout(
() =>
axios
.get(`players/%23${member.tag}`)
.then(res => resolve(res))
.catch(err => reject(err)),
RATE_LIMIT_BASE * idx
);
});
});
Promise.all(promises).then().catch(); // etc;
在评论中,正如marvel308所说,
this could fail in certain cases, since setTimeout just guarantees that the code will execute atleast 100ms after setTimeout is called, they could still queue up in the event queue in case some CPU intensive task was blocking the call stack
感谢 marvel308 的建议
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);
});