I want to write the output of getStatus
into the object results
.
Does anyone have an idea how I can do this? I have already tried a few things, e.g. using push
to fill an array, but that didn't really work either.
Source:
const ping = require('ping');
const results = {}; // my empty object
const ips = {
server1: '8.8.4.4',
server2: '1.103.1.1',
server3: '8.8.8.8',
};
const serverStatus = (hosts) => {
return new Promise((resolve, reject) => {
ping.sys.probe(hosts, function (isAlive) {
const msg = isAlive ? `host ${hosts} is alive` : `host ${hosts} is dead`;
resolve(msg);
});
});
};
const getStatus = (ips) => {
for (const [key, value] of Object.entries(ips)) {
serverStatus(value)
.then((s) => console.log(s)) // the promise output
.catch((e) => console.error(e));
}
};
getStatus(ips);
Output:
host 8.8.4.4 is alive
host 8.8.8.8 is alive
host 1.103.1.1 is dead