javascript - 如何在 Node.js 中的每个 API 调用之间进行延迟
问题描述
我的目标是对数据列表进行多次 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>
推荐阅读
- android - 谷歌地图未显示在标签片段中
- azure - 是否可以在不停机的情况下将登台转换为生产?
- java - iText 2用不同的字体替换段落中的文本
- angular - 预计至少有 0 个参数,但得到了 2 个
- c - 检测文件中的字符并在 C 中拆分字节数组
- javascript - 使用带有 Typescript 的量角器获取所有元素属性?
- java - 在 catch 块中使用 return?
- php - MapMyIndia 行车距离矩阵 API
- scala - Flink:如何持久化和恢复一个 ValueState
- reactjs - SetState 空数组与对象的 Json 响应数组?