Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!