0

I am new to node and having some headaches with async programming. I have a simple script pinging devices on my network. Now I want to build the following: if one of the devices is on the network then how do I handle the callback so that the decision is only made once all the pings are terminated?

var exec = require('child_process').exec;

function doThePing(ipaddy){
    exec("ping " + ipaddy, puts);
}

function puts(error, stdout, stderr) { 
    console.log(stdout);

    if (error !== null){
        console.log("error!!!!");
    }
    else{
        console.log("found device!")
    }
}

function timeoutFunc() {
    doThePing("192.168....");
    doThePing("192.168....");
    //if all pings are successful then do..
    setTimeout(timeoutFunc, 15000);
}

timeoutFunc();
skirtle
  • 27,868
  • 4
  • 42
  • 57
  • refer this may be help you https://stackoverflow.com/questions/28849900/how-to-wait-for-function-to-finish-before-continuning-in-node-js – Rijo Oct 19 '17 at 06:19
  • [Promise](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise) can help you, create a promise for each ping callback, then wait all compete – aspark Oct 19 '17 at 06:29
  • [Promise](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise) can help you, create a promise for each ping callback, then wait all compete – aspark Oct 19 '17 at 06:31
  • What information do you need in your final callback? Do you need to know which pings succeeded or is it sufficient just to know that they have all completed? – skirtle Oct 19 '17 at 08:44
  • would be great to know which ping succeeded but also have it asynchronous. Is there a way the callback can reference to the caller so it knows if it handles the outcome of ping 1 or ping 2? –  Oct 19 '17 at 10:59

1 Answers1

1

You could "Promisify" the exec call, taken from the docs

const util = require('util');
const exec = util.promisify(require('child_process').exec);

Update your ping function to return the promise

function doThePing(ipaddy){
  return exec("ping " + ipaddy);
}

Then wrap all the resulting promises in a Promise.all

Promise.all([doThePing("192.168...."),doThePing("192.168....")).then(function(values) {
  // all calls succeeded
  // values should be an array of results
}).catch(function(err) {
  //Do something with error
});
Chris Phillips
  • 673
  • 5
  • 10
  • interesting...so will the pinge be executed in sequence or parallel? –  Oct 19 '17 at 07:27
  • i cannot make this run: (node:9600) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Command failed: ping 192.168.178.22 (node:9600) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. Besides, to me it looks like return exec is not returning anything? –  Oct 19 '17 at 08:09
  • oaky, i fixed that error but still it is not doing what i want. basiclly i want to ping x devices in parallel, get to know who is online and continue therafter. above code waits for calls to finish but still i cannot evaluate the exec output... –  Oct 19 '17 at 09:09
  • in the `then` the parameter `values` should be an array of the results. – Chris Phillips Oct 19 '17 at 19:06