Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda