AmazingDayToday
AmazingDayToday

Reputation: 4272

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

Upvotes: 0

Views: 530

Answers (2)

Lee
Lee

Reputation: 447

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.

Upvotes: 0

Andrés
Andrés

Reputation: 783

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

Upvotes: 1

Related Questions