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

141
Vistas
Can I error handle await Promise.all with a try-catch block?

I have the following piece of code:

    const [groupedMeditations, meditationPreferences] = await Promise.all([
        getMeditationsByGroup(),
        getAllPreferences(),
    ]);

but currently errors are not handled in any way. Would it be correct to surround that piece of code with try-catch like this:

    try {
        const [groupedMeditations, meditationPreferences] = await Promise.all([
            getMeditationsByGroup(),
            getAllPreferences(),
        ]);
    } catch (error) {
        // error handle
    }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

try/catch is a correct way to catch errors from Promise.all, but at the same time, it will ignore all fulfilled requests once only a single request failed, which is not ideal if you still want to have other successful requests' data instead of logging errors, so I'd suggest you use Promise.allSettled

With this solution, it will keep all requests including failed ones and successful ones

const [groupedMeditations, meditationPreferences] = await Promise.allSettled([
   getMeditationsByGroup(),
   getAllPreferences(),
]);

A possible result can be

[
   {status: "fulfilled", value: "successful value"}, // your 1st request passed
   {status: "rejected",  reason: "Error: an error"} //your 2nd request failed
]

From that response, you can filter or log errors

Just one side note that Promise.allSettled does not work for IE, so you need to have polyfill to overcome that

Hopefully, it's helpful for your case :D

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