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

704
Vistas
Async-Await: How to get data in multiple await calls even when there is an error in one?

So I have this async function where I make multiple calls to NodeJS's DNS Api. The first two calls return data but because of the third one, the catch block triggers and I don't get the data from the first two.

As it all depends on the type of query(netflix.com), some of the await operations might throw an error because of no data. But what I want is I want to get data from the other calls even if there are errors from some of them.

Here's the code:

const dnsResolve = async () => {
    const query                 = 'netflix.com';

    try{
        const aRecord           = await dns.resolve4(query);
        const aaaaRecord        = await dns.resolve6(query);
        const cnameRecord       = await dns.resolveCname(query);
     
        console.log(aRecord);
        console.log(aaaaRecord);
        console.log(cnameRecord);
    }catch(err) {
        console.log(err);
    }
}

dnsResolve();

The above code returns

Error: queryCname ENODATA netflix.com
    at QueryReqWrap.onresolve [as oncomplete] (internal/dns/promises.js:167:17) {
  errno: undefined,
  code: 'ENODATA',
  syscall: 'queryCname',
  hostname: 'netflix.com'
}

Individual outputs from the first two queries:

aRecord: [
  { address: '51.73.1.12' },
  { address: '55.83.62.128' },
  { address: '12.12.82.118' }
]
aaaaRecord:  [
  { address: '2406:da200:ff00::222c0:598a' },
  { address: '24206:da00:ff00::22c2:44203' },
  { address: '2406:da0e0:ff00::36de2:41fd' }
]

How I want to display the data:

[
  { address: '51.73.1.12' },
  { address: '55.83.62.128' },
  { address: '12.12.82.118' },
  { address: '2406:da200:ff00::222c0:598a' },
  { address: '24206:da00:ff00::22c2:44203' },
  { address: '2406:da0e0:ff00::36de2:41fd' },
  { NO CNAME RECORDS }
]

Please note that there will be around 6 calls to the DNS API. It cannot be determined which one will trigger an error because of no data. It totally depends on the query.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

This is the very use case Promise.allSettled is for:

const results = await Promise.allSettled([
    dns.resolve4(query),
    dns.resolve6(query),
    dns.resolveCname(query),
]);
for (const {status, value, reason} of results) {
    if (status === "fulfilled") {
        // Promise was fulfilled, use `value`...
    } else {
        // Promise was rejected, see `reason` for why...
    }
}

Or if you only want the successful results:

const results = await Promise.allSettled([
    dns.resolve4(query),
    dns.resolve6(query),
    dns.resolveCname(query),
]);
const values = results.filter(({status}) => status === "fulfilled")
                      .map(({value}) => value);
// ...use `values`...
over 4 years ago · Santiago Trujillo 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