简体   繁体   中英

React ES6: calling async function from another function not defined

This is where I call register() :

this.register();

Here's the register() :

register = async () => {
    await login();
    await create();
}

Here are the ones I call:

login = () => {
    console.log('test');
}

Ideally, what I want is to finish login() first, then go to create() .

Error I get:

Uncaught (in promise) ReferenceError: login is not defined

As stated in the comments, you will probably need to complete the question with more context. In general, in Javascript, if you are working with classes, you will need to bind the context:

this.login = this.login.bind(this)

and then call this.login()

Moreover, in the case of react/redux , if your login function is not in your component you will probably have to connect your component and tell Redux what is the function you'll use, as explained here: Redux - call action from a function

I had this issue when I was calling a normal function inside an Async function. Even though the async function was called directly on the button click, it still needed binding ( this.function= this.function.bind(this) ). The function I was calling did not need Binding itself, which wasn't consistent with what I've encountered before with binding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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