Calling async function inside non-async function in React-Native - Firebase

21,503

Solution 1

Like @lonesomeday said, this.functionOne() is all I needed. My issue was I wasn't binding this properly in the onPress of the TouchableOpacity I was calling it from.

Solution 2

Like this:

async functionOne() {
    asyncStuffHappens
}

functionTwo() {
    (async () => {
        await this.functionOne();
    })();
}

This is called an IIFE (Immediately-invoked function expression). It's a function executed right after it's created

Share:
21,503
wvicioso
Author by

wvicioso

Updated on February 26, 2020

Comments

  • wvicioso
    wvicioso about 4 years

    This is what I'm trying do in React-Native. Async functions are making calls to firebase.

    async functionOne() {
        asyncStuffHappens
    }
    
    functionTwo() {
        this.functionOne();
    }
    

    this.functionOne(); is undefined. I'm not sure how to call an async function from another function.

  • lonesomeday
    lonesomeday about 7 years
    This is completely unnecessary here, as the return value is never used. this.functionOne() is all you need.
  • Vincent D'amour
    Vincent D'amour about 7 years
    The code provided didn't show everything. I'm guessing that a value might be returned or that another action needs to be done after the async one. Otherwise, he/she wouldn't ask this question.
  • lonesomeday
    lonesomeday about 7 years
    Then this may or may not be a good solution. An IIAFE is not normally necessary.
  • Vincent D'amour
    Vincent D'amour about 7 years
    You're right on this, an IFEE is not necessary in this case