如何使axios GET请求等待?

时间:2019-02-14 06:15:59

标签: javascript arrays node.js axios fetch

嗨,我在使用axios / fetch GET和一个URL时遇到问题,该URL的参数通过数组循环对其值进行迭代

axios / fetch不遵循数组的顺序,仅返回最先出现的响应。

我该如何解决?

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)的结果

所以基本上它要么以

的形式出现

a,b,c(需要)

b,c,a

a,c,b

c,b,a

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

a,b,c始终

3 个答案:

答案 0 :(得分:2)

您可以通过以下方式使用递归:

function fetchRecursively(int currentStep, int n) {
    var wttURL = "https://whattomine.com/coins.json?" + algo + "=true" + "&factor%5B" + algo + "_hr%5D=" + hashrate[currentStep]
    fetch(wttURL)
        .then((resp) => resp.json()) // Transform the data into json
        .then(function(data) {
            console.log(data.coins.Ethereum.btc_revenue);
            if (currentStep < n) {
                fetchRecursively(currentStep + 1, n);
            }
        })
}

并替换为循环:

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)
  })

与此:

fetchRecursively(0, vhr.length);

答案 1 :(得分:2)

使用Promise.all,以保留订单。

在for循环中,iam创建三个访存承诺并进行链接,然后以json格式获取响应并将其推入数组。

最后,我正在使用Promise.all在prdefined formar中获取数据

const fetch = require("node-fetch");

algo = "eth" // algorithm for wtt

hashrate = ['250', '100', '50']

console.log(hashrate)

var fetchUrl =[]
for (var i = 0; i < vhr.length; i++){

return fetchUrl.push(fetch("https://whattomine.com/coins.json?" + algo + "=true" + "&factor%5B" + algo + "_hr%5D=" + hashrate[i]).then(response => response.json())
}


Promise.all(fetchUrl)
.then(function(data) {
  data.forEach(a => a.coins.Ethereum.btc_revenue)
  })

答案 2 :(得分:1)

选项1:Promise.all

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

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));

但是,它失败的原因是第一个承诺被拒绝。因此,如果您的数组很大,或者需要显示任何数据,则不应使用此方法。另外,这将只保留客户的订单!您的后端对订单一无所知,因为调用未按顺序进行。

选项2:异步/等待

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

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

相关问题