Inside submitButton event, I would like to check if there are any users registered or not.
Suppose there are 5 users registered:
getUsers = function () {
return new Promise(function (resolve, reject) {
resolve(5);
reject(0);
});
}
Inside the checkregisteredUsers, I call getUsers and then I check if there are any users. If there are any users, then return true, otherwise false. I set state of UsersFound.
checkRegisteredUsers = () => {
return this.getUsers().then(result => {
return this.setState({ UsersFound: result > 0 ? true : false };
}).catch(reason => {
console.log(reason);
});
}
Now, inside the function submitButton, I would like to check for true or false, if there were users registered.
submitButton = () => {
this.checkRegisteredUsers().then(result => {
this.setState({ UsersFound: result > 0 ? true : false });
});
if(!this.state.UsersFound){
return; **//exit code here and the rest of the code below should not execute and show the dialog box**
}
// run some other code
}
When I call submitButton, the state of UsersFound is not set yet. If I click a second time then it is set. How can I set it inside a promise? Or How can I check for true/false inside the submitButton using promises?
EDIT:
The question is: how can I return a boolean from a promise as I would like to check something like this:
submitButton = () => {
if(this.checkRegisteredUsers()){
return; //the rest of the code below should not execute
}
//if check users is true then the code below should not execute
// some other code
}
I don't want a promise returned, I want a boolean.