我是Node JS的新手,目前正在学习Promise和await / async。我尝试了下面的代码,但无法弄清楚如何使代码等待功能托管完成。我也曾尝试过诺言,但迫不及待。
var ping = require('ping');
var hoststatus
var hosts = ['google.com'];
async function test()
{
var connected = await hostping(hosts);
console.log('connected--------------', connected)
connected.then(function(hoststatus) {
console.log('hoststatus--------------', hoststatus)
if (hoststatus == 1) {
console.log('faaaaaaaail-------------')
} else {
console.log('passssssssssssssssss-----------')
}
});
}
async function hostping(hosts) {
console.log('hosts-----------', hosts)
await hosts.forEach(async function(host) {
await ping.sys.probe(host, async function(isAlive) {
var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
console.log(msg);
if (isAlive == 'false') {
hoststatus = 1
}
});
});
return hoststatus;
}
test()
答案 0 :(得分:1)
这应该做您想要的,我们使用for .. of loop循环访问主机。
ping库还具有一个Promise包装器,可让您跳过使用回调的操作。
您还可以使用Promise.all一次完成所有的ping和探测,但是我不相信这是您想要做的。
如果您想一次执行所有ping操作,我已经包含了一个使用Promise.all的hostpingVer2。
const ping = require('ping');
const hosts = ['google.com', 'amazon.com', 'itdoesntexist'];
async function test() {
console.log('hosts: ', hosts)
const results = await hostping(hosts);
console.log('ping results: ', results);
}
async function hostping(hosts) {
const results = [];
for(let host of hosts) {
let probeResult = await ping.promise.probe(host);
results.push( { host, hoststatus: probeResult.alive ? 0: 1, alive: probeResult.alive } );
}
return results;
}
async function hostpingVer2(hosts) {
const probeResults = await Promise.all(hosts.map(host => ping.promise.probe(host)));
return probeResults.map(probeResult => {
return { host: probeResult.host, hoststatus: probeResult.alive ? 0: 1, alive: probeResult.alive }
});
}
test();
答案 1 :(得分:0)
您正在forEach
内部使用await,因为每个都不支持诺言,因此该方法不起作用。考虑将其更改为for..of
或promise.all
。
此外,您的probe函数是不返回promise的回调样式。您需要将其包装在一个等待中的承诺中。
function probe(host) {
return new Promise((resolve, reject) => {
ping.sys.probe(host, function (isAlive) {
const msg = isAlive ? `host ${host} is alive` : `host ${host} is dead`;
console.log(msg);
resolve(isAlive);
});
});
}
async function hostping(hosts) {
console.log("hosts-----------", hosts);
for(const host of hosts) {
// eslint-disable-next-line no-loop-func
// eslint-disable-next-line no-await-in-loop
const isAlive = await probe(host);
if (isAlive === "false") {
hoststatus = 1;
}
}
return hoststatus;
}
答案 2 :(得分:0)
要做你想做的事,你应该使用ping的promise包装器,也应该使用for..of,因为forEach不适用于异步。
下面是对代码的修改,以使流程按预期工作:
const ping = require('ping');
const hosts = ['google.com', 'yahoo.com'];
async function test()
{
const status = await hostping(hosts);
if (status) {
console.log('All hosts are alive');
} else {
console.log('Some/all hosts are dead');
}
}
function hostping(hosts) {
let status = true;
return new Promise(async function(resolve, reject) {
for (const host of hosts) {
const res = await ping.promise.probe(host);
var msg = `host ${host} is ${res.alive ? 'alive' : 'dead'}`;
console.log(msg);
if (!res.alive) { status = false; }
}
console.log('all host checks are done, resolving status');
resolve(status);
});
}
test()
如果您有兴趣了解有关异步的更多信息,请阅读以下不错的文章: https://www.coreycleary.me/why-does-async-await-in-a-foreach-not-actually-await/
https://itnext.io/javascript-promises-and-async-await-as-fast-as-possible-d7c8c8ff0abc