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

133
Vistas
How to sequentially run promises with error handling

I need to call 3 requests in sequence, all of them with 3 resolutions

  • if request is 200 and response is ok, call another request
  • if request is 200, but response is bad, do something
  • if request is not successful, do something else

Happy path would look like this:

const firstCallResponse = await this.service1.call1();
if (firstCallResponse) {
  const secondCallResponse = await this.service2.call2();
  if (secondCallResponse) {
    const thirdCallResponse = await this.service3.call3();
    if (thirdCallResponse) {
      console.log('sequence finished successfully);
    }
  }
}

And it doesn't look that bad, but if I'll try to add those two fallbacks for every request, the code will become very messy.

try {
  const firstCallResponse = await this.service1.call1();
  if (firstCallResponse) {
    try {
      const secondCallResponse = await this.service2.call2();
      if (secondCallResponse) {
        try {
          const thirdCallResponse = await this.service3.call3();
          if (thirdCallResponse) {
            console.log('sequence finished successfully');
          } else {
            console.log('do something, as third call response is not ok');
          }
        } catch {
          console.log('do something as third call failed');
        }
      } else {
        console.log('do something, as second call response is not ok');
      }
    } catch {
      console.log('do something as second call failed');
    }
  } else {
    console.log('do something, as first call response is not ok');
  }
} catch {
  console.log('do something as first call failed');
}

Is there a way to make this code more readable or elegant? The code above would work, but it doesn't look good and it's extremely hard to read. Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

First of all you can wrap more than one promise with try/catch if you want to do the same thing when they fail. Also accept argument in catch blocks since they can tell you where error originated / more info about it aka

try {
  // await promise
} catch (errorName) {
  // handle error
}

Other than that main thing I would recommend is reversing your logic with checking the results what to do next and adding return statements.

try {
  const firstCallResponse = await this.service1.call1();
  if (!firstCallResponse) {
    console.log('do something as first call failed');
    return;
  }

  const secondCallResponse = await this.service2.call2();
  if (!secondCallResponse) {
    console.log('do something as second call failed');
    return;
  }

  // ...
  // if no errors do something with all results
} catch {
  console.log('do something as first call failed');
}

Another tip I would like to add for end is if your second call does not require anything from results of first call you can group them all in one promise with

await Promise.all([
  this.service1.call1(),
  this.service2.call2()
])
about 4 years ago · Juan Pablo Isaza 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