<bdo id='wCXCC'></bdo><ul id='wCXCC'></ul>

    1. <small id='wCXCC'></small><noframes id='wCXCC'>

      <legend id='wCXCC'><style id='wCXCC'><dir id='wCXCC'><q id='wCXCC'></q></dir></style></legend>
    2. <i id='wCXCC'><tr id='wCXCC'><dt id='wCXCC'><q id='wCXCC'><span id='wCXCC'><b id='wCXCC'><form id='wCXCC'><ins id='wCXCC'></ins><ul id='wCXCC'></ul><sub id='wCXCC'></sub></form><legend id='wCXCC'></legend><bdo id='wCXCC'><pre id='wCXCC'><center id='wCXCC'></center></pre></bdo></b><th id='wCXCC'></th></span></q></dt></tr></i><div id='wCXCC'><tfoot id='wCXCC'></tfoot><dl id='wCXCC'><fieldset id='wCXCC'></fieldset></dl></div>
      <tfoot id='wCXCC'></tfoot>

        你如何让 axios GET 请求等待?

        时间:2023-10-01
        <legend id='bVRvO'><style id='bVRvO'><dir id='bVRvO'><q id='bVRvO'></q></dir></style></legend>

              <tbody id='bVRvO'></tbody>
            <tfoot id='bVRvO'></tfoot>
          1. <small id='bVRvO'></small><noframes id='bVRvO'>

                <i id='bVRvO'><tr id='bVRvO'><dt id='bVRvO'><q id='bVRvO'><span id='bVRvO'><b id='bVRvO'><form id='bVRvO'><ins id='bVRvO'></ins><ul id='bVRvO'></ul><sub id='bVRvO'></sub></form><legend id='bVRvO'></legend><bdo id='bVRvO'><pre id='bVRvO'><center id='bVRvO'></center></pre></bdo></b><th id='bVRvO'></th></span></q></dt></tr></i><div id='bVRvO'><tfoot id='bVRvO'></tfoot><dl id='bVRvO'><fieldset id='bVRvO'></fieldset></dl></div>
                  <bdo id='bVRvO'></bdo><ul id='bVRvO'></ul>

                  本文介绍了你如何让 axios GET 请求等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  您好,我在使用 axios/fetch GET 时遇到问题,该 URL 具有一个参数,该参数通过数组循环迭代其值

                  Hi I'm having a problem with using axios / fetch GET with a URL that has a parameter that iterates through an array loop for its values

                  axios/fetch 不遵循数组的顺序,只返回先出现的响应.

                  axios / fetch doesn't follow the order of the array and just returns whichever response comes first.

                  我该如何解决这个问题?

                  How would I fix this?

                  const fetch = require("node-fetch");
                  
                  algo = "eth" // algorithm for wtt
                  
                  hashrate = ['250', '100', '50']
                  
                  console.log(hashrate)
                  
                  
                  for (var i = 0; i < vhr.length; i++){
                  
                  var wttURL = "https://whattomine.com/coins.json?" + algo + "=true" + "&factor%5B" + algo + "_hr%5D=" + hashrate[i]
                  
                  
                  
                  fetch(wttURL)
                  .then((resp) => resp.json()) // Transform the data into json
                  .then(function(data) {
                    console.log(data.coins.Ethereum.btc_revenue)
                    })

                  目前的输出是 250 (a)、100 (b) 或 50 (c) 的结果

                  The output for this currently is either the results for 250 (a), 100 (b) or 50 (c)

                  所以基本上它要么会出现

                  So basically it would either come out as

                  a、b、c(需要)

                  b、c、a

                  a、c、b

                  c、b、a

                  等等

                  但我希望它按照顺序输出所以应该是

                  But I want it to output according to the order so it should be

                  a ,b ,c 总是

                  推荐答案

                  选项 1:Promise.all

                  简而言之:您可以使用 Promise.all() 如果您需要特定的订单.您创建一个充满承诺的数组,将其传递给 Promise.all(),您将获得一个包含已解决承诺的数组.

                  In short: you can use Promise.all() if you need a specific order. You create a an array filled with promises, pass it to Promise.all() and you'll get an array with resolved promises.

                  const fetch = require("node-fetch");
                  
                  algo = "eth" // algorithm for wtt
                  hashrate = ['250', '100', '50']
                  
                  wttURL = [];
                  
                  for (var i = 0; i < vhr.length; i++) {
                      wttURL.push(fetch("https://whattomine.com/coins.json?" + algo + "=true" + "&factor%5B" + algo + "_hr%5D=" + hashrate[i]))
                  }
                  
                  Promise.all(wttURL)
                      .then(responses => responses.forEach(response => console.log(response)))
                      .catch(err => console.log(err));
                  

                  但是,由于第一个拒绝的承诺的原因,它失败了.因此,如果您有一个大数组或需要显示任何数据,则不应使用此方法.此外,这将只保留客户端上的订单!您的后端不会知道任何关于订单的信息,因为调用没有按顺序完成.

                  However, it fails with the reason of the first promise that rejects. So if you have a big array or you need to display any data you should not use this method. In addition, this would keep the order on the client only! Your backend would not know anything about the order since the calls are not done in order.

                  选项 2:异步/等待

                  您可以改用 async/await.您将 await 每个结果,如果其中任何一个失败,您不必在意,因为其余的仍然可以成功.此外,您的后端也可以跟踪订单.

                  You could use async/await instead. You would await each result and if any of them fails you do not care since the rest still can succeed. Also, your backend would be able to keep track of the order too.

                  async function getData() {
                      for (var i = 0; i < vhr.length; i++) {
                          let wttURL = "https://whattomine.com/coins.json?" + algo + "=true" + "&factor%5B" + algo + "_hr%5D=" + hashrate[i]
                          await fetch(wttURL)
                                  .then(resp => resp.json()) // Transform the data into json
                                  .then(data => console.log(data.coins.Ethereum.btc_revenue))
                                  .catch(err => console.log(err));
                      }
                  }
                  

                  这种方法会保留客户端和后端的原始顺序(如果您记录它).但是,它比第一个解决方案慢,因为它不会继续下一个 fetch,直到 promise 被解决.

                  This approach preserves the original order on the client and backend (if you log it). However, it is slower than the first solution since it does not continue with the next fetch until the promise is resolved.

                  这篇关于你如何让 axios GET 请求等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用来自服务器的表单数据在客户端上获取响应 下一篇:如何在 NodeJs 中的 API 获取请求成功时呈现到新的 HTML 页面?

                  相关文章

                  <i id='vQfyR'><tr id='vQfyR'><dt id='vQfyR'><q id='vQfyR'><span id='vQfyR'><b id='vQfyR'><form id='vQfyR'><ins id='vQfyR'></ins><ul id='vQfyR'></ul><sub id='vQfyR'></sub></form><legend id='vQfyR'></legend><bdo id='vQfyR'><pre id='vQfyR'><center id='vQfyR'></center></pre></bdo></b><th id='vQfyR'></th></span></q></dt></tr></i><div id='vQfyR'><tfoot id='vQfyR'></tfoot><dl id='vQfyR'><fieldset id='vQfyR'></fieldset></dl></div>
                • <tfoot id='vQfyR'></tfoot>

                    <bdo id='vQfyR'></bdo><ul id='vQfyR'></ul>

                      <small id='vQfyR'></small><noframes id='vQfyR'>

                      <legend id='vQfyR'><style id='vQfyR'><dir id='vQfyR'><q id='vQfyR'></q></dir></style></legend>