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

340
Visualizações
JS MAY (behave differently on different runtime) not catch the rejected promise in setTimeout

In the following code.

const p = Promise.reject();;
setTimeout(() => {
    p.catch(() => console.log("Error caught"));
}, 0);

I expected Error caught is printed, but when I run it in Node v16.15.1, it crashes with UnhandledPromiseRejection 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 "undefined".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

The error is not caught.

Then I tried in chrome 102.0.5005.63 but I got Error caught printed as expected.

Now I'm very confused about this behavior.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

There must be at least one rejection handler attached to a Promise at the moment that the Promise rejects to avoid unhandled rejections. The Promise will have rejected (and the error will have been generated) by the time the timeout callback runs.

Your code produces an unhandled rejection in Chrome as well, as expected - add an unhandledrejection listener to see (though, of course, unlike Node, this doesn't crash the page)

window.addEventListener('unhandledrejection', () => {
  console.log('Unhandled rejection detected');
});
const p = Promise.reject();;
setTimeout(() => {
    p.catch(() => console.log("Error caught"));
}, 0);

Adding a .catch after the Promise has rejected will result in the .catch handler running - but the engine will already have seen that at the moment the Promise rejected, the rejection wasn't handled.

Best approach to this sort of thing - add a .catch handler synchronously when the Promise is created, not after any possible asynchronous action, otherwise the Promise might reject before the handler gets attached.

about 4 years ago · Santiago Gelvez 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