如何进行多个 API 调用,每个调用之间有延迟 Node.js
How to make multiple API calls with a delay between each in Node.js
我的目标是多次 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
,让它更有用。
使用块,最佳性能:
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>
简单点。为循环中的每个请求注入延迟。
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)
}
有多种策略可以处理过多的请求,哪种策略可以在不违反目标服务器速率限制的情况下获得最佳吞吐量,这完全取决于目标服务器的测量方式和强制执行事情。除非记录在案,否则您将不得不进行实验。最安全(也可能是最慢)的策略是 运行 您的请求按顺序延迟,并适当调整延迟时间。
运行 每个请求之间有延迟
您可以 运行 您的请求按顺序使用 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 > 1 且 N < 所有请求的时间的请求
如果您想在 N 大于 1(通常为 3 或 4)但少于所有请求的情况下一次 运行 N 请求,请参阅 mapConcurrent()
[=13] =].这是否像您所做的那样可行,完全取决于目标服务器及其测量和执行的内容。
您 运行 每秒 N 次请求的实际速率限制
对于直接控制每秒请求数的实际速率限制,请参阅此答案中的 rateLimitMap()
:Choose proper async method for batch processing for max requests/sec。
对于任何喜欢面向对象风格的人来说,创建一个可重用的装饰器可以更具可读性。
function delayAsync(delayMs = 0): any {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const prevMethod = descriptor.value;
descriptor.value = async function (...args: any[]) {
await Promise.resolve(
new Promise<void>((resolve) => {
setTimeout(() => { resolve(); }, delayMs);
}),
);
return prevMethod.apply(this, args);
};
return descriptor;
};
}
class Test {
@delayAsync(500)
async test() {
console.log('test');
}
}
const test = new Test();
(async () => {
for await (const i of Array(10).keys()) {
await test.test();
}
})();
你可以测试一下here
我的目标是多次 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
,让它更有用。
使用块,最佳性能:
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>
简单点。为循环中的每个请求注入延迟。
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)
}
有多种策略可以处理过多的请求,哪种策略可以在不违反目标服务器速率限制的情况下获得最佳吞吐量,这完全取决于目标服务器的测量方式和强制执行事情。除非记录在案,否则您将不得不进行实验。最安全(也可能是最慢)的策略是 运行 您的请求按顺序延迟,并适当调整延迟时间。
运行 每个请求之间有延迟
您可以 运行 您的请求按顺序使用 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 > 1 且 N < 所有请求的时间的请求
如果您想在 N 大于 1(通常为 3 或 4)但少于所有请求的情况下一次 运行 N 请求,请参阅 mapConcurrent()
[=13] =].这是否像您所做的那样可行,完全取决于目标服务器及其测量和执行的内容。
您 运行 每秒 N 次请求的实际速率限制
对于直接控制每秒请求数的实际速率限制,请参阅此答案中的 rateLimitMap()
:Choose proper async method for batch processing for max requests/sec。
对于任何喜欢面向对象风格的人来说,创建一个可重用的装饰器可以更具可读性。
function delayAsync(delayMs = 0): any {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const prevMethod = descriptor.value;
descriptor.value = async function (...args: any[]) {
await Promise.resolve(
new Promise<void>((resolve) => {
setTimeout(() => { resolve(); }, delayMs);
}),
);
return prevMethod.apply(this, args);
};
return descriptor;
};
}
class Test {
@delayAsync(500)
async test() {
console.log('test');
}
}
const test = new Test();
(async () => {
for await (const i of Array(10).keys()) {
await test.test();
}
})();
你可以测试一下here