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

357
Vistas
In javascript, why doesn't await return an error when a promise catches?

I like async/await but I don't understand why we have to wrap our await in a try/catch block to catch errors. Why not make it so await returns some type of standardized error object and then we test for that?

Then we could do something like:

let results = await somePromise();
results?.err ? doSomething() : handleErr()
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The point of async/await is to allow asynchronous/promise-based code to be written in a manner more similar to synchronous code. Synchronous code uses try/catch to handle exceptions, so it by the principal of least surprise, await should too.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Error handling is up to the author. If you don't control some code that throws, and you want it to not throw, wrap it with something that catches but doesn't throw. In your own code, you should feel free to return error objects rather than throw.

Just be aware that others have come to expect throw/catch as a convention.

// this one throws errors 
import fnNotControlledByMe from 'external_lib'  

// use this one as a wrapper, and don't throw
// this and any other function you write can opt not to throw
async function fnControlledByMe() {
  try {
    let results = fnNotControlledByMe();
    return results;
  } catch (err) {
    return { err };
  }
}

async function opFunction() {
  let results = await fnControlledByMe();
  results?.err ? doSomething() : handleErr()
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Why not make it so await returns some type of standardized error object and then we test for that?

Because that assumes you always want to test for the error. In many (most!) cases you don't, you want to let the exception propagate. This leads to a lot boilerplate code (like the node callback pattern with error parameters), ignored errors by ignorant (unaware) programmers, and refactoring hazards.

As @SeanSutherland pointed out in his answer, this also makes asynchronous error handling symmetric to synchronous error handling, with the same expectations.

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