i have an easy Ajax request who return true or false according to the result, like this :
function myRequest(){
$.ajax({
url: "/",
method: "GET",
contentType: "application/json",
dataType: 'json',
}).done(function (data) {
if (data['code'] !== "success") {
return false;
}
else {
return true;
}}).fail(function (xhr, textStatus, errorThrown) {
return false;
});
}
if(myRequest()){
//do someting
}
When myRequest() is executed for the if statement, it's doesn't wait the Ajax result and is never validate. I read about promise here and i tried but it still the same issue.
Ty !
[EDIT]
So i follow your replies and i just retry with promises. It's work better :
function myRequest(){
return new Promise((resolve, reject) =>{
$.ajax({
url: "/",
method: "GET",
contentType: "application/json",
dataType: 'json',
}).done(function (data) {
if (data['code'] !== "success") {
resolve(false);
}
else {
resolve(true);
}}).fail(function (xhr, textStatus, errorThrown) {
rejected(errorThrown);
});
});
}
myRequest().then(function (val) {
if(val){
return true;
}
});