Lets say I have a login function
login = () => {
let url = baseURL + '/user/login?_format=json';
let data = {
"name": this.state.email,
"pass": this.state.password
};
return axios({
url,
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
withCredentials: true,
credentials: 'same-origin'
})
.then(function(result) {
console.log('result:', result);
this.setState({csrfToken: result.data.csrf_token});
this.setState({logoutToken: result.data.logout_token});
return result;
})
.catch(error => console.log('error:', error));
};
I then want to call the function onSubmit in React like follows. If the function returns an error for any reason. How do I prevent the next function in this case api.login()
from being run?
{api => (
<Form
onSubmit={async e => {
e.preventDefault();
await this.login();
api.login()
}}
>
<input/>
</Form>
Does a try/catch make sense in this case? I've tried several options including an inline try catch and the function runs no matter what happens as soon as the promise from this.login();
returns either an result or error.