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
Author by
wvicioso
Updated on February 26, 2020Comments
-
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 about 7 yearsThis is completely unnecessary here, as the return value is never used.
this.functionOne()
is all you need. -
Vincent D'amour about 7 yearsThe 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 about 7 yearsThen this may or may not be a good solution. An IIAFE is not normally necessary.
-
Vincent D'amour about 7 yearsYou're right on this, an IFEE is not necessary in this case