简体   繁体   English

如何使用 axios 同时发出数百个 get 请求

[英]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.

相关问题 多个axios在socket.io中获取请求。 通过`socket.emit`同时发送两个事件 - Multiple axios get requests in socket.io. Sending two events via `socket.emit` at the same time 如何使用 Reactjs 和 Axios 正确发出 API 请求? - How to make API requests correctly with Reactjs and Axios? 如何使用 axios 在反应中发出请求 - How to use axios to make requests in react 如何从 axios GET 请求呈现表? - How to render a table from axios GET requests? 我如何通过我的反应应用程序上的 Axios 从 Imgur 的 API 发出 GET 请求 - How do i make a GET requests from Imgur's API via Axios on my react app 如何使用 componentDidMount() 发出多个 axios.get() 请求并将第一个响应值分配给第二个? - How to make multiple axios.get() requests with componentDidMount() and assign a response value of the 1st to the 2nd? 如何使用Moxios在同一测试中拦截两个axios请求 - How to intercept two axios requests in the same test using moxios 如何在 React.js 中使 Axios 请求在移动设备上工作 - How to make Axios requests work on mobile in React.js 如何使用Javascript中的动态链接数组生成多个Axios请求? - How to make multiple Axios requests using a dynamic array of links in Javascript? REACT如何使用Redux和Axios(使用promise中间件)发出AJAX请求? - REACT How to make AJAX requests with Redux and Axios (with promise middleware)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM