0

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)

Patrick Keane
  • 663
  • 3
  • 19
  • 41
  • you cannot return value from ajax request callbacks – A. Wolff Mar 03 '14 at 18:29
  • the 3 first conditions returns false why u don't use "or" ? if (condition || condition || condition) {isValide = false} or you can just declar isValidate=false and work in that condition where i returns true ? use 3 lines instead of all those lines! and then i'll would be clear to find your error ;) – Alaeddine Mar 03 '14 at 18:43
  • Good note @Alaeddine and its something I will do when I clean up my code. For now though this is outside the scope of the question. – Patrick Keane Mar 05 '14 at 11:40

2 Answers2

3

Try:

function stepIsValid(stepName, callback) {
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;
            callback(isValid);
        })
       .error(function( data ) {
            isValid = false;
            callback(isValid);
       });
}
else {
    isValid = false;
    callback(isValid);
}

And use it with:

stepIsValid(stepName, function(valid) {
    if(valid) {
        //Do Something
    }
    else doSomethingElse();
});
meir shapiro
  • 647
  • 7
  • 16
0

decrease your function

try this :

function stepIsValid(stepName) {
var 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 ) {
            return isValid;
       });
}
}
Alaeddine
  • 1,571
  • 2
  • 22
  • 46