Skip to content

Instantly share code, notes, and snippets.

@zhoukekestar
Created March 28, 2017 03:43
Show Gist options
  • Save zhoukekestar/090d93e0afde6a38603bb019b77cee12 to your computer and use it in GitHub Desktop.
Save zhoukekestar/090d93e0afde6a38603bb019b77cee12 to your computer and use it in GitHub Desktop.
Fastest IP for you!
const exec = require('child_process').exec;
const ping = (ip) => {
return new Promise((resolve, reject) => {
exec(`ping ${ip}`, (err, stdout, stderr) => {
if (err) {
reject(err);
return;
}
resolve({ip, stdout});
});
});
}
var ips = `
8.7.198.45
203.98.7.65
54.192.72.90
46.82.174.68
59.24.3.173
54.192.72.87`.split(/\s{1,}/g).filter((ip) => ip.length > 0);
Promise.race(ips.map((ip) => ping(ip))).then(({ip, stdout}) => {
console.log(`${ip} is best`);
console.log(stdout)
}).catch((e) => {
console.log(e);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment