I've been running into an issue with my JavaScript code not executing in the expected order. It seems to be skipping straight to the if(!hadError)
condition before handling any errors that may occur, resulting in hadError always being true. I understand that this has to do with how JavaScript runs things asynchronously, but I'm unsure of how to resolve it.
Here's the code snippet:
var email = $('#email').val();
var password = $('#password').val();
var hadError = false;
if(email != "" && password != ""){
auth.signInWithEmailAndPassword(email, password).catch(function(error) {
hadError = true;
console.log(hadError);
var errorCode = error.code;
var errorMessage = error.message;
$('#login-error').text(errorMessage);
});
if(!hadError){
success();
}
}