【发布时间】:2019-07-08 02:48:28
【问题描述】:
您好,我在使用 axios / fetch GET 时遇到问题,该 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 总是
【问题讨论】:
-
试试 rxjs forkjoin 函数
-
它遵循顺序,但并非所有请求都同时完成——这就是异步操作的基本思想。每当他们完成时,他们就完成了。如果您需要等待所有结果,请使用Promise.all
-
@AlbertiBuonarroti 把它变成答案
-
另请注意,JS 实现并不总是按输入顺序返回数组。
标签: javascript arrays node.js axios fetch