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
Calling .finally on a promise that is try/catch handled elsewhere causes UnhandledPromiseRejection in nodejs

I've recently encountered a weird behavior with nodejs. I'll explain with the following example:

let's say we have two functions, foo and bar, foo creates a promise, calls .finally() on it, and returns it. bar on the other hand, wraps the promise with a try/catch.

const foo = () => {
    const promise = new Promise((resolve, reject) => {
        reject('my-error');
    });
    
    promise.finally(() => {
        console.log('at foo');
    });

    return promise;
}

const bar = async () => {
    try {
        const result = await foo();
    } catch (e) {
        console.log('at bar: ', e);
    }
}

bar();

the output would be:

at foo
at bar:  my-error
node:internal/process/promises:279
        triggerUncaughtException(err, true /* fromPromise */);
        ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "my-error".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

From the output, we can see that the error is handled by bar function, yet an UnhandledPromiseRejection error is thrown.

However, if we replace .finally() with .catch() not such error happen.

const foo = () => {
    const promise = new Promise((resolve, reject) => {
        reject('my-error');
    });

    promise.catch(() => {
        console.log('at foo');
    });

    return promise;
}

I'm not sure what's happening over here; I'd appreciate it if someone could help me visualize what's happening.

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

0

then , catch , and finally create new promises that resolve based on the promise you call and the result of the handler function you pass to them. (If resolution a is a bit unfamiliar, check out my blog post on promise terminology .)

If you want to finally use the promise created in foo , return the resulting promise instead of using catch as follows

 const foo = (props) => {
 return new Promise((resolve, reject) => {
 reject('my-error');
 }).finally(() => {
 console.log('at foo');
 });
}

const bar = async (e) => {
 try {
 await foo();
 } catch (error) {
 console.log('at bar: ', error);
 }
}

bar();

almost 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