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

198
Visualizações
Aborting multiple-step promise chains with custom error handlers

I'm writing a multi-step async sequence that involves different possible errors at each step. I want to abort the whole sequence if an error occurs.

I am trying to express this as a linear sequence using Promise.then() and Promise.catch(). Here is the pseudocode:

step1()
.catch(errorAtStep1 => handlerErrorAtStep1)
.then(resultOfStep1 => step2(resultOfStep1))
.catch(errorAtStep2 => handlerErrorAtStep2)
.then(resultOfStep2 => step3(resultOfStep2))
...

I understand that I need to throw something in each error handler to avoid getting back into the then(result) parts. When I throw, though, the execution ends up in the next catch(error) block, which was expecting an error that's related to its corresponding step, not that other thing that I threw.

My question is: what are clean ways to handle this, that would minimize distraction when reading the code?

I came up with this class:

class AbortChainError extends Error {
  static chain(handler) {
    return function(error) {
      if (error instanceof AbortChainError) throw error
      handler(error)
      throw new AbortChainError()
    }
  }
}

that allows the catch() blocks to be written as:

.catch(AbortChainError.chain(errorAtStep2 => handlerErrorAtStep2))

with the additional need to write a final, empty catch() block at the end of the sequence.

Can we do better?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you put this into an async function, I'd have the error handlers return nothing, and then you can do each step one-by-one and see if the result exists or not - if it doesn't, there was an error, and you can return.

const doStuff = async () => {
  const r1 = await step1().catch(handlerErrorAtStep1);
  if (!r1) return;
  const r2 = await step2(r1).catch(handlerErrorAtStep2);
  if (!r2) return;
  // etc

It's not the most DRY, but I think it's the easiest to understand.

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