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

147
Vistas
Why does the stack trace of an Error that is returned by a Promise seem to be incomplete?

While rethrowing an Error of a rejected Promise, I noticed that the stack trace seem to be incomplete.

For example:

// returns a Promise that is later on rejected.
function a() {
    return new Promise((resolve, reject)=>{
        setTimeout(()=>reject(new Error("a")), 2000);
    });
}

async function b() {
    await a();
}

async function c() {
    await b();
}

// testing
async function test() {
    let printError = (error)=>console.log(error.stack);

    await a().catch(printError);
    await b().catch(printError);
    await c().catch(printError);
}

test();

All three function calls in test() print the following:

Error: a
    at <anonymous>:4:31

I would have expected something like:

Error: a
    at <anonymous>:4:31
    at new Promise (<anonymous>)
    at a (<anonymous>:3:12)
    at b2 (<anonymous>:13:15)
    at c2 (<anonymous>:21:15)
    at test (<anonymous>:32:2)

Shouldn't the calling functions be part of the stack trace?



I tested this code in Google Chrome.

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

0

Async stack traces (the ones you get in production) only work with async functions so when you have a function that isn't async and just uses promises the chain won't stitch. You can read more on the implementation in the design document the fix fortunately is pretty simple:

// the function has to be async
async function a() {
    // isolate areas that have to use raw promises 
    await new Promise((resolve, reject)=>{
        setTimeout(()=>resolve(), 2000);
    });
    // throw the error from inside the function

    throw new Error('a');
}

Which happily logs:

Error: a
    at a (<anonymous>:6:11)
    at async b (<anonymous>:10:5)
    at async c (<anonymous>:14:5)
    at async test (<anonymous>:23:5)
about 4 years ago · Juan Pablo Isaza Denunciar

0

I could be wrong but I believe its because you create an anonymous function within setTimeout, which would then be thrown for the error.

Maybe not the cleanest, but for every Error I put in a logPrefix, and do something like the below. Just means you can easily debug an error.

const logPrefix = '[my-function-name]'
throw new Error(`${logPrefix} - The error maybe with ${error} injected`)

If my function is within a class or something I might do the below

const logPrefix = '[className.methodName]'
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