I am trying to set up a promise that would only return the outcome after the ajax call
However in my code the outcome returns null before the ajax is completed, from console log i can also see that it is returned before completion of the checkuser function.
How can i achieve this , do I have to make a timeout loop waiting for the outcome to change value to 0 or 1 and set a timeout limit?
function checkUser() {
return new Promise((resolve, reject) => {
$.ajax({
type: "POST",
url: 'checkUserAvail.php',
data: {value: value},
success: function (data) {
resolve(data);
},
error: function (error)
{
reject(error);
}
})
})
}
checkUser()
.then((data) => {
console.log(data);
if (data == 1)
{
outcome= 1;
}
if (data == 0)
{
outcome= 0;
}
})
.catch((error) => {
alert("An error ocurred");
})
console.log(outcome);
return outcome;