Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda