【发布时间】:2021-12-28 07:35:08
【问题描述】:
我运行这段代码我想立即发送请求,但在所有 for 循环执行之后。我想在循环中做其他事情,而不是等待答案。每当答案出现时,我都会做出反应
var fetchUrl = require("fetch").fetchUrl;
for (var i = 0; i < 10; i++) {
console.log(i);
checkbalance(i);
}
function checkbalance(req) {
var urlCheckBalance =
"https://api.etherscan.io/api?module=account&action=balancemulti&address=" +
req +
"&tag=latest&apikey=<api key>";
// source file is iso-8859-15 but it is converted to utf-8 automatically
fetchUrl(urlCheckBalance, function (error, meta, body) {
console.log(body.toString());
});
}
响应在这里:所有序列号生成。 fetch 之后运行。
0
1
2
3
4
5
6
7
8
9
{"status":"0","message":"NOTOK","result":"Error! Invalid address format"}
{"status":"0","message":"NOTOK","result":"Error! Invalid address format"}
{"status":"0","message":"NOTOK","result":"Error! Invalid address format"}
{"status":"0","message":"NOTOK","result":"Max rate limit reached"}
{"status":"0","message":"NOTOK","result":"Max rate limit reached"}
我想要这个结果,例如:
0
1
{"status":"0","message":"NOTOK","result":"Error! Invalid address format"}
2
3
4
5
6
7
{"status":"0","message":"NOTOK","result":"Error! Invalid address format"}
8
9
{"status":"0","message":"NOTOK","result":"Error! Invalid address format"}
{"status":"0","message":"NOTOK","result":"Max rate limit reached"}
{"status":"0","message":"NOTOK","result":"Max rate limit reached"}
【问题讨论】:
-
你的环境支持 ES6 吗?
-
如果我使用它,它会起作用吗?以及如何?
标签: node.js http request fetch