Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
How do i write the result of a Javascript Promise into an object which is outside the function?

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
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!