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

232
Views
How to resolve an asynchronous function when returning a hoisted array?

I'm trying to return describedIpList after all async operations are resolved. I can see a few ways to solve this without a promise, but I am interested in learning how to do this in best practice with a promise, do I really have to wrap another promise around my function and if so, please show me how to do this correctly.

 const ipResolver = (ips) => {
    const describedIpList = [];

    ips.map((ip) => {
        Promise.all([
            client.country(ip),
            client.city(ip)
        ])
            .then(response => {
                [Country, City] = response;
                describedIpList.push({
                    ip,
                    countryCode: Country.country.isoCode,
                    postalCode: City.postal.code,
                    cityName: City.city.names.en,
                    timeZone: City.location.timeZone,
                    accuracyRadius: City.location.accuracyRadius,
                });
                console.log(describedIpList);
            })
            .catch(err => describedIpList.push({ip, error: err.error}));
        return describedIpList;

    });

To reiterate. I am looking to return describedIpList after ips.map() resolves and then return a response res.json({fullyDescribedIps: ipResolver(ips)});

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You'll need two Promise.alls - one to wait for all ip requests to resolve, and one to wait for the country and city of all of those to resolve.

const ipResolver = ips => Promise.all(
    ips.map(ip => Promise.all([
        client.country(ip),
        client.city(ip)
    ])
        .then(([Country, City]) => ({
            ip,
            countryCode: Country.country.isoCode,
            postalCode: City.postal.code,
            cityName: City.city.names.en,
            timeZone: City.location.timeZone,
            accuracyRadius: City.location.accuracyRadius,
        }))
        .catch(err => ({ ip, error: err.error }))
    )
);
about 4 years ago · Juan Pablo Isaza Report

0

async function ipInfo(ip) {
  try {
    const [Country, City] = await Promise.all([
      client.country(ip),
      client.city(ip),
    ]);
    // const [Country, City] = await client.countryAndCity(ip);
    return {
      ip,
      countryCode: Country.country.isoCode,
      postalCode: City.postal.code,
      cityName: City.city.names.en,
      timeZone: City.location.timeZone,
      accuracyRadius: City.location.accuracyRadius,
    }
  } catch (err) {
    return { ip, error: err.error };
  }
}

const ipResolver = Promise.all(ips.map(ipInfo));
// const ipsInfo = await Promise.all(ips.map(ipInfo));
about 4 years ago · Juan Pablo Isaza Report
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!