【发布时间】:2019-12-13 03:17:59
【问题描述】:
我想将 Promise 函数的结果推送到一个数组中,但是该数组仍然是空的。有人可以帮忙吗This is what I would like to achieve at the end, update my ejs file from the promise function. However, nothing happens once I do this.
var Data = [];
raw.forEach(function (host) {
ping.promise.probe(host.ip).then(function (res) {
if(res.alive){
const input = {
"Host": i.host,
"IP": i.ip,
"Status": "Alive",
"Avg": res.avg
}
Data.push(input);
}
else {
const input = {
"Host": i.host,
"IP": i.ip,
"Status": "Dead",
"Avg": res.avg
}
Data.push(input);
}
});
});
console.log(Data);
【问题讨论】:
-
Data已填充,只是您在之前记录它。 -
那我应该做些什么改变来保存数组中的数据呢?谢谢! @briosheje
-
如前所述,数据已填充,您只是在在填充之前记录它,您不能 将在未来中填充的日志数据。您必须在函数中移动使用
Data的代码,并在.then回调内部 调用它。检查上面的重复链接,所有内容都在该帖子中进行了解释。 -
我想填充数组“数据”并在其他地方使用它。怎么做?谢谢!
标签: javascript express promise ip