你如何让axios GET请求等待?

How do you make axios GET request wait?

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

axios / fetch 不遵循数组的顺序,只是 returns 以先到者为准。

我该如何解决这个问题?

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 总是

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

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 循环:

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

使用Promise.all,以保持顺序。

在 for 循环中,我正在创建三个获取承诺并链接,然后以 json 格式获得响应并将它们推送到数组。

最后,我使用 Promise.all 以预定义格式获取数据

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

选项 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

您可以改用 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 直到承诺得到解决。