Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

236
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda