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

190
Vistas
Better way to call conditionally promises

I have a scenario where I have "Promise A" and "Promise B". I need to call both promises only if the variable "value" is false, else, I only need to call the Promise B.

So, I wrote this code:

if(!value){
            PromiseA.then((newValue)=>{
                PromiseB.then((newValue)=>{
                    //...
                })
            })
        }
        else{
            PromiseB.then((value)=>{
                //....
            })
        }

This code works fine, but I'm wondering if there is a better way to accomplish this than using "if-else".

This for have clean code.

Thanks in advance.

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

0

Inside an async function, await the first Promise if you need to.

const firstValue = value ?? await PromiseA;
const resultOfPromiseB = await PromiseB;
// do stuff with resultOfPromiseB
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can wrap the second promise in a function if you want to avoid code repetition.

function callPromiseB() {
   PromiseB.then((value)=>{
      //....
   })
}

        if(!value){
            PromiseA.then((newValue)=>{
                callPromiseB()
            })
        }
        else{
            callPromiseB()
        }
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