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

73
Vistas
lose stack trace when using Promise.then vs. async-await

I have the following snippet implemented with Promise.then:

function b() {
  return Promise.resolve()
}


function c() {
   throw new Error('error here')
}

const a = () => {
    b().then(() => c());
};


a()

The stack trace output from the error only included function c

VM57:6 Uncaught (in promise) Error: 343
    at c (<anonymous>:6:9)
    at <anonymous>:10:17

However, if I switch this to async-await as in:

function b() {
  return Promise.resolve()
}


function c() {
   throw new Error('error here')
}

const a = async () => {
    await b()
    c()
};


a()

This time the stack trace included the whole call stack with function a

Uncaught (in promise) Error: 343
    at c (<anonymous>:6:9)
    at a (<anonymous>:3:5)

I tested this in Chrome dev console. I wonder what caused the difference and does this mean one should stick with async-await for better debugbility? Also are there any resources I can learn more about this difference?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This is expected.

In the then version, the function a has ran to completion before the then callback executes. That anonymous callback is the only function on the call stack at that moment. When c is called, and the rejection occurs, a is not on the call stack.

In the await version, the function a is first suspended (it returns a promise), but then when b() resolves, the execution context of a is restored and execution of a resumes. So when the rejection occurs in c, a is on the call stack.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Its anonymous because its called inside an anonymous function: () => c(). In the other example its called inside a

If you name your callback function, you will see the name inside the error log:

const a = () => {
  b().then(function test() {
    c();
  });
};
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