ajax - Javascript code running in order -


im having issues js code not running in order. skips if(!haderror) before executes catch when there error haderror equal true. know has javascript , how runs things simultaneously im not sure how fix it. here code:

var email = $('#email').val(); var password = $('#password').val(); var haderror = false; // var unlocked = false;  if(email != "" && password != ""){   auth.signinwithemailandpassword(email, password).catch(function(error) {     // console.log(haderror);     haderror = true;     console.log(haderror);     var errorcode = error.code;     var errormessage = error.message;     $('#login-error').text(errormessage);     // unlocked = true;   });   if(!haderror){     success();   } } 

firebase methods asynchronous , return promise's. can .then() on after method call execute code after promise returned, e.g.

auth.signinwithemailandpassword(email, password).then((user) => {      //if want, `user` firebase.user     if(!haderror) success(); }).catch(function(error) {     haderror = true;     console.log(haderror);     var errorcode = error.code;     var errormessage = error.message;     $('#login-error').text(errormessage); }); 

depending on method may not need check haderror.

for further reference: https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/promise/then

in cases can pass second method then called if promise rejected, firebase method api refers catch error handling.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -