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

208
Vistas
How to fire single await/async based on a condition?

I have a NodeJS app and I have two async blocks that I am calling but the condition is that if the first block returns a value, I don't want to fire the second block. This is how my code looks:

//BLOCK 1
await canFunction1HandleID(id)
.then( () => {
  const result = await functionOne(id);
    if(!result){
      //throw error
    }

    return result;
})

//BLOCK 2
await canFunction2HandleID(id)
.then( () => {
  const result = await functionTwo(id);
    if(!result){
      //throw error
    }

    return result;
})

If the first block returns a value, I just want to return that value and not have the second block execute. But right now both blocks are executed, one after another. Any idea why and how can I achieve that I am trying to do here?

TIA.

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

0

Just put the first value into a variable?

const result1 = await canFunction1HandleID(id)
    .then(() => {
        const result = await functionOne(id);
        if (!result) {
            //throw error
        }
        return result;
    });

if (!result1) {
    const result2 = await canFunction2HandleID(id)
        .then(() => {
            const result = await functionTwo(id);
            if (!result) {
                //throw error
            }
            return result;
        });
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

If canFunction1HandleID and canFunction2HandleID are functions that return Promise<bool>, what about the following?

//BLOCK 1
if (await canFunction1HandleID(id)) {
  const result = await functionOne(id);

  if (!result) {
    //throw error
  }

  return result;
}

//BLOCK 2
if (await canFunction2HandleID(id)) {
  const result = await functionTwo(id);

  if (!result) {
    //throw error
  }

  return result; 
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

If the first block returns a value, I just want to return that value and not have the second block execute.

Suppose what you expect is: if functionOne has valid return, use it; otherwise try functionTwo and throw error if no valid return neither.

The code to implement the logic can be simply like:

const result = await functionOne(id).catch(()=>{}) || await functionTwo(id).catch(()=>{});
if (!result) {
    // throw error
}
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