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

201
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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