[英]How to make hundreds of get requests at the same time with axios
I'm making a React application using an external API.我正在使用外部 API 制作一个 React 应用程序。 But I face a small issue atm.
但我面临一个小问题。 I need to make a single GET request to every ID that I have.
我需要对我拥有的每个 ID 发出一个 GET 请求。 It's possible that I need to make up to 1000 GET requests and the problem of that is the time it takes to get all the data.
我可能需要最多 1000 个 GET 请求,而问题在于获取所有数据所需的时间。 Is there a way of making let's say 1000 GET requests without waiting for each of them to resolve?
有没有办法让我们说 1000 个 GET 请求而不等待每个请求解决? I have this but it takes for ever.
我有这个,但它需要永远。
for(const match of totalMatchesWithPremades){
try {
const matchStats: ISats = await axios.request<IMatchStats>({
url: `https://API/match/${match.match_id}/stats`,
method: 'get',
headers: {
accept: 'application/json',
Authorization: `Bearer ${API_KEY}`,
},
});
console.log(JSON.stringify(matchStats,null,2))
} catch (err) {
console.log(err);
}
}
Probably you need to check out something like bulk
routes for the API.可能您需要检查诸如 API 的
bulk
路由之类的内容。 So eg you can set the array of match_id
s and make only one request and get the array of data.因此,例如,您可以设置
match_id
数组并仅发出一个请求并获取数据数组。
Otherwise you need to wait for each request to be executed.否则你需要等待每个请求被执行。
I discovered I can use Promise.allSettled()
like this我发现我可以像这样使用
Promise.allSettled()
Promise.allSettled(arr.map((match)=>{
const matchStats: ISats = await axios.request<IMatchStats>({
url: `https://API/match/${match.match_id}/stats`,
method: 'get',
headers: {
accept: 'application/json',
Authorization: `Bearer ${API_KEY}`,
},
});
console.log(JSON.stringify(matchStats,null,2))
} catch (err) {
console.log(err);
}
})
This will execute all Promises one after another and return an array with if they got fullfilled or rejected.这将一个接一个地执行所有 Promise,并返回一个数组,如果它们被填充或被拒绝。 I made 400 requests in like 5 seconds intead 4minutes
我在 5 秒内而不是 4 分钟内发出了 400 个请求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.