<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 请求等待?

        How do you make axios GET request wait?(你如何让 axios GET 请求等待?)
        <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 请求等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)

                    <tbody id='vQfyR'></tbody>
                  <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>