I'm already look in the What is the scope of variables in JavaScript? but it looks different. Variable which i will take are inside "function->for loops->function->end of if statement".
The code originally from net-ping
Here is the code :
var ping = require ("net-ping");
netping();
value = 0;
function netping() {
var targets = ['8.8.8.8','8.8.4.4'];
var session = ping.createSession ();
for (var i = 0; i < targets.length; i++) {
session.pingHost (targets[i], function (error, target, sent, rcvd) {
var ms = rcvd - sent;
if (error)
if (error instanceof ping.RequestTimedOutError)
console.log (target + ": timeout " + ms + " ms");
else
console.log (target + ": " + error.toString () + " (ms="+ ms + ")");
else
value = target + ": " + ms + " ms";
console.log(value);
// output
// 8.8.4.4: 6 ms
// 8.8.8.8: 7 ms
});
}
}
console.log(value); // output 0
module.exports = netping;
How can i get the variable value from function netping to show them outside the function?