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

136
Views
Return boolian in parent function after child promise resolves

I am working in a framework that requires the parent function to return true or false. Within that function, I'd like to run an async operation.

How do I make the parent function return true or false after an async action?

Example of what doesn't work:

   function parent() {
     const child = new Promise();
     child.then(
       function (success) { return true },
     )
   }

The problem is that returning is happining within the context of the child.then() function.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This necessarily requires that the parent function be async as well, unless you want to block the main thread, which would be considered an anti-pattern.

Do this:

const parent = async () => {
    const childResponse = await child();
    if (childResponse === "something") {
        return true;
    } else {
        return false;
    }
};

Do NOT do this:

const parent = () => {
    let promiseResult, parentReturnValue;

    child.then(result => {
        promiseResult = result;
        if (promiseResult === "something") {
            parentReturnValue = true;
        } else {
            parentReturnValue = false;
        }
    });

    while (promiseResult === undefined) {
        // block the main thread
    }
    
    return parentReturnValue;
};
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!