简体   繁体   English

在 React-Native 的非异步函数中调用异步函数 - Firebase

[英]Calling async function inside non-async function in React-Native - Firebase

This is what I'm trying do in React-Native.这就是我在 React-Native 中尝试做的。 Async functions are making calls to firebase.异步函数正在调用 firebase。

async functionOne() {
    asyncStuffHappens
}

functionTwo() {
    this.functionOne();
}

this.functionOne(); is undefined.未定义。 I'm not sure how to call an async function from another function.我不确定如何从另一个函数调用异步函数。

Like this:像这样:

async functionOne() {
    asyncStuffHappens
}

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

This is called an IIFE (Immediately-invoked function expression) .这称为IIFE(立即调用的函数表达式) It's a function executed right after it's created这是一个在创建后立即执行的函数

Like @lonesomeday said, this.functionOne() is all I needed.就像@lonesomeday 说的, this.functionOne()就是我所需要的。 My issue was I wasn't binding this properly in the onPress of the TouchableOpacity I was calling it from.我的问题是我没有约束力this正确的TouchableOpacity我的onPress从调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM