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

202
Visualizações
Is it anti-pattern to run asynchronous callbacks in catch blocks?

Is it an anti-pattern to run asynchronous callbacks in error catching like this?

await something()
   .catch(async (err) => {
      console.error(err);
      await otherStuff();
   });
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If your intent is to have that whole expression there never reject - that is, to avoid the need for

try {
  await something()
   .catch(async (err) => {
      // ...
} catch(e) {
  // handle
}

Then what you're doing right now isn't safe if there's any chance of the otherStuff call rejecting. If otherStuff might reject, then you should make sure to catch possible errors it throws, eg

await something()
   .catch(async (err) => {
      console.error(err);
      await otherStuff();
   })
   .catch(handleOtherStuffErrors)

Though having an async function with only a single await isn't that useful - consider instead just returning the Promise:

await something()
   .catch((err) => {
      console.error(err);
      return otherStuff();
   })
   .catch(handleOtherStuffErrors)

Chaining catches like that is pretty odd, but it's not impossible that that's what one's logic requires, so don't make a blanket statement about whether it's inherently bad.

about 4 years ago · Juan Pablo Isaza Relatório

0

The way you do it is. In a promise you should return the promise instead of making the callback async and awaiting it.

I don't think it is since you can call .then behind the catch. Sometimes an throw just goes into a diffrent branch of operations but at the end its preference.

await something()
   .catch((err) => {
      console.error(err);
      return otherStuff();
   }).then(res => {
  // work with data
});
about 4 years ago · Juan Pablo Isaza 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