I'm currently building a Nodejs script which should interact with a web server and a local network device. In order to make the program as reliable as possible I want to do a simple ping test to check if the network device can be reached.
var ping = require('ping');
function pingtest(host) {
ping.sys.probe(host, function (isAlive) {
var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
console.log(msg);
return isAlive;
});
}
let pingSuccessful = pingtest('192.168.178.100');
console.log(pingSuccessful);
console.log('Should not executed before pingtest has finished.');
The output on the console is the following:
undefined
Should not executed before pingtest has finished.
host 192.168.178.100 is dead
The problem is that the script execution should pause until pingtest() has finished and returned the result. My goal is to console.error() a message and stop the script if this test failed. I already tried it with async await and the other code examples at https://github.com/danielzzz/node-ping but unfortunately this didn't work as expected.