我的目标是为一个数据列表进行多次api调用.假设我有以下代码。
const axios = require('axios');
const axiosRequests = [];
const strings = ['a', 'b', 'c'];
for (let str of strings) {
axiosRequests.push(axios.get(`https://www.apiexample.com/get/?cfg=json&value=${str}`))
}
最简单的解决方法是应用下面的方法。
let responseArray;
try {
responseArray = await Promise.all(axiosRequests);
} catch (err) {
console.log(err);
}
responseArray.map(response => {
//make something with the response
{
但我在API中遇到的问题是: HTTP 429 Too Many Requests
响应状态码,这意味着API限制了一段时间内的请求数量。
我想在每个请求之间增加一个延迟。
我怎样才能做到这一点?
你可以调用 series
. 不过,我建议 using chunks
,使其更加实用。
使用chunk,性能最佳。
const delay = (ms = 1000) => new Promise((r) => setTimeout(r, ms));
const getInChunk = async function (items, chunkSize) {
let results = [];
let chunkPromises = [];
let chunkResults = [];
for (let index = 0; index < items.length; index++) {
if (index % chunkPromises === 0) {
chunkPromises = [];
chunkResults.push(await Promise.all(chunkPromises));
} else {
chunkPromises.push(
axios.get(`https://jsonplaceholder.typicode.com/todos/${items[index]}`).then(res => res.data)
);
}
}
// last chunk
if (chunkPromises.length) {
chunkResults.push(await Promise.all(chunkPromises));
}
// flatten
chunkResults.forEach(chunk =>{
results = results.concat(chunk)
})
console.log(results)
return results;
};
async function main() {
const strings = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const results = await getInChunk(strings, 5);
console.log(results);
}
main();
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script>
简单。
const axios = require("axios");
const delay = (ms = 1000) => new Promise((r) => setTimeout(r, ms));
const getInSeries = async (promises) => {
let results = [];
for (let promise of promises) {
results.push(await delay().then(() => promise));
}
return results;
};
const getInParallel = async (promises) => Promise.all(promises);
async function main() {
const strings = [1, 2, 3, 4];
const promises = strings.map((id) =>
axios
.get(`https://jsonplaceholder.typicode.com/todos/${id}`)
.then((res) => res.data)
);
const results = await getInSeries(promises);
console.log(results);
}
main();
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script>
性能友好的系列。 循环一次 O(N)
.
const delay = (ms = 1000) => new Promise((r) => setTimeout(r, ms));
const getTodosSeries = async function (items) {
let results = [];
for (let index = 0; index < items.length; index++) {
await delay();
const res = await axios.get(
`https://jsonplaceholder.typicode.com/todos/${items[index]}`
);
results.push(res.data);
}
return results;
};
async function main() {
const strings = [1, 2, 3, 4];
const results = await getTodosSeries(strings);
console.log(results);
}
main();
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script>
有多种策略来处理过多的请求,哪种策略能让你获得最好的吞吐量而不违反目标服务器的速率限制,完全取决于目标服务器到底是如何衡量和执行的。 除非那是有记录的,否则你只能做实验。 最安全(也可能是最慢)的策略是按顺序运行你的请求,并在它们之间有一个延迟,然后根据情况调整延迟时间。
按顺序运行,每个请求之间有延时
您可以按顺序运行您的请求,并在延迟承诺上使用 await 来及时分离它们。
const axios = require('axios');
function delay(t) {
return new Promise(resolve => setTimeout(resolve, t));
}
async function getResults() {
const results = [];
const strings = ['a', 'b', 'c'];
for (let str of strings) {
await delay(1000);
let data = await axios.get(`https://www.apiexample.com/get/?cfg=json&value=${str}`);
results.push(data);
}
return results;
}
getResults.then(results => {
console.log(results);
}).catch(err => {
console.log(err);
});
一次运行N个请求,其中N > 1和N < 你的所有请求。
如果你想一次运行N个请求,其中N大于1(通常是3或4),但小于你所有的请求,那么参见 mapConcurrent()
在 本回答. 这是否是可行的,而不是像你做的那么多,完全取决于目标服务器和它究竟是测量和执行。
每秒钟运行N个请求的实际速率限制
对于直接控制每秒请求的实际速率限制,请参见 rateLimitMap()
在这个答案中。选择适当的异步方法进行批处理,以满足最大的请求量秒。.
简单一点。在循环中的每个请求中注入一个延迟。
for (const [i, v] of ['a', 'b', 'c'].entries()) {
setTimeout(()=>{
axiosRequests.push(axios.get(`https://www.apiexample.com/get/?cfg=json&value=${str}`))},
i * 100)
}