skip to Main Content

So in my code I have a math equations that depends on an async call to Firebase. So when I get the values back I calculate the score inside a then statement, but I can’t get the function it’s inside to return the calculated value. So for instance if I have something like:

function parentFunction(){
   let result = childFuntion()
}

function childFunction(){
    
    let query = new Promise((resolve, reject) => {
         let query = asyncFirebaseCall()
         resolve(query)
    }
    Promise.all([listofQueries])
    .then((res) => {
     let result = _doMathStuff(listofQueries)
     //I know this is not correct but im putting the return here
     return result
     }

}

how do i get the value of result inside the parent function?

2

Answers


  1. The main issue is that you’re trying to return a value from an asynchronous operation as if it was synchronous. Instead, you should return a promise from childFunction and handle it in parentFunction.

    My suggested code:

    function parentFunction(){
       childFunction()
       .then(result => {
           console.log(result);
           // now you have the result inside the parentFunction
           // do whatever you want with it
       })
       .catch(error => {
           console.error("Error fetching data:", error);
       });
    }
    
    function childFunction(){
        // if asyncFirebaseCall() itself returns a promise
        return new Promise((resolve, reject) => {
            let query = asyncFirebaseCall();
            // if there are multiple queries, push them all into an array and then use Promise.all
            Promise.all([query])
            .then(responses => {
                let result = _doMathStuff(responses);
                resolve(result);
            })
            .catch(error => {
                reject(error);
            });
        });
    }
    

    A few points:

    asyncFirebaseCall() should return a promise.

    I used Promise.all() even if there’s one promise for demonstration purposes. If you have multiple promises, you can add them in the array.

    The promise inside childFunction is resolved with the result from the _doMathStuff function.

    In the parentFunction, you call childFunction and handle its returned promise.

    By doing this, you are correctly handling the asynchronous nature of your operations and can get the result of your calculations in the parentFunction when it’s ready.

    Login or Signup to reply.
  2. I see two problems in your code at first glance:

    • You’re missing a top-level return statement inside your childFunction function.
    • You’re missing a then inside your parentFunction function.

    And as Jaromanda already commented, you don’t need to construct your own Promise objects.

    So with those two fixed, the code would look like:

    function parentFunction(){
        childFunction().then((result) => { // 👈
            ...
        })
    }
    
    function childFunction(){    
        let query = asyncFirebaseCall();
        ...
        return Promise.all([listofQueries]) // 👈
        .then((res) => {
             let result = _doMathStuff(listofQueries)
             //I know this is not correct but im putting the return here
             return result
         }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search