0

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.

  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Luca Kiebel Nov 03 '19 at 16:02

2 Answers2

1

You simply can not return from a callback. You can make use of Promise. Refactor the code like this:

function pingtest(host) {
  return new Promise((resolve, reject) => {
    ping.sys.probe(host, function (isAlive) {
        var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
        console.log(msg);

        resolve(isAlive);
    });
  });
}

pingtest('192.168.178.100').then((pingSuccessful) => {
  console.log(pingSuccessful);
});

Or, you have to do everything inside ping.sys.probe callback.

Shihab
  • 2,641
  • 3
  • 21
  • 29
  • Thank you for the quick response. I had this before but if you have any other code outside of the .then() method this is executed directly. I was wrong with my understanding of this behavior because I thought that calling such a function immediately pauses the execution until the result has come. Thank you! – cocktailbar90 Nov 03 '19 at 16:19
  • If anything dependent on `pingSuccessful`, you have to put it inside `.then`. Because of javascript's non blocking nature, it would run all the synchronous code first without waiting for any async execution. – Shihab Nov 03 '19 at 16:23
  • This was exactly my "mistake". Thank you for the explanation! – cocktailbar90 Nov 03 '19 at 16:26
  • You can learn more about **javascript Promise (async/await)** to take full control over your code. – Shihab Nov 03 '19 at 16:29
0

yes this is async flow, you can use async/await to solve this issue

function pingtest(host) {
    return newPromise((res, rej) => {
        ping.sys.probe(host, function (isAlive) {
            var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
            console.log(msg);
            res(isAlive)
        });
    }
}

const startPing = async () => {
    let pingSuccessful = await pingtest('192.168.178.100');

    console.log(pingSuccessful);
    console.log('Should not executed before pingtest has finished.');
}

startPing()
Eslam Abu Hugair
  • 1,162
  • 1
  • 11
  • 23
  • `async` in-front of `pingtest` function is unnecessary since the function itself is return a promise. – Shihab Nov 03 '19 at 16:12