Answer the question
In order to leave comments, you need to log in
Result of ajax function as condition value in JavaScript?
There is a function1 that calls $.ajax and receives a true|false response. There is a function2 that fires on an event and checks several values for a condition, including the value of function1. But since function1 executes an asynchronous request, the conditions in function2 always evaluate to false. The question is how to make the correct execution of conditions with an asynchronous request?
function fun1 (callback) {
$.ajax({
...
success: function(res) {
callback(res);
}
})
}
function fun2(val1, val2) {
var result;
if (val1) result=true;
else if (fun1(function(res) {return res;})) result=trrue;
else if (val2) result=true;
else result=false;
return result;
}
Answer the question
In order to leave comments, you need to log in
In such cases it is very convenient to use promis'y. For example q.js
https://github.com/kriskowal/q
as you've been told above, promises are perfect for this. By the way, they are in jquery , and all ajax methods return exactly the promise.
Example
Are you sure you are getting a boolean value in response? Surely not text?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question