如何使axios GET请求等待? [英] How do you make axios GET request wait?

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

问题描述

我在使用axios/fetch GET和一个URL时遇到问题,该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.

我该如何解决?

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)

所以基本上它要么以

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

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

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.

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请求等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆