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

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

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 Report

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