Attempting to work with functions that return true or false, and also have some ajax included.
I call a function expecting a true or false reply:
if(stepIsValid(stepName) == true) {
//Do Something
}
else doSomethingElse();
The function 'stepIsValid()' does a number of checks and then requests additional info via AJAX. Then returns true or false:
function stepIsValid(stepName) {
var isValid = true;
if(condition) {
isValid = false;
}
if(condition) {
isValid = false;
}
if(condition) {
isValid = false;
}
if (pub && isbn) {
$.post( "/index.php/mypath", { isbn: isbn, name: pub })
.done(function( data ) {
$("#resultElement").val(data);
isValid = true;
return isValid;
})
.error(function( data ) {
isValid = false;
return isValid;
});
}
else {
isValid = false;
return isValid;
}
My problem is that when stepIsValid is called, jQuery doesn't wait for a response, assumes false and calls doSomethingElse() before true is returned.
What is the best way to overcome this problem?
Code does in fact return true but not quick enough.
Both functions are in separate files.
(assume condition, pub, isbn are all plumbed in and available)