0

How do I get email_exists to reflect the value of exists outside of the Promise, so I can use it in a conditional?

 async function verify_email(email) {
    var exists = 0;

    await $.ajax({
        type: "POST",
        url: "/settings/ajax-verify-email",
        data: {'email': email},
        success: function (response) {
            exists = response;
        }
    });
    return exists;
}

var email_exists = 0;

verify_email(email).then(function(exists) {
    console.log(exists); // shows 1
    email_exists = exists;
}

console.log(email_exists); // shows 0
Ping
  • 191
  • 1
  • 12
  • If you can use `async` and this is a frontend project, you should also have access to the `fetch` built-in function for executing http requests. The `fetch` function supports promises natively. Otherwise, you need to "promisify" the ajax call using the promise constructor. – CRice Mar 07 '23 at 22:11
  • I need to hit the database and check to see if an email address exists before continuing with form validation. – Ping Mar 07 '23 at 22:17

0 Answers0