The else block is always executed before checking the if condition

The else block is always executed before checking the if condition. The continueToNextValidation () function, executed before (clusterId) validation, exits completely. How to wait for the Validation (clusterId) function to complete and then evaluate the if-else block based on its result.

function validateUpgradateStatus()
{
    var target = $("#selectcluster").data("ejDropDownList");
    var clusterId = target.model.itemValue;
    var isValidated = Validation(clusterId);
    if(isValidated )
     Error("error msg");
    else
        proceedToNextValidation();
}
function Validation(id) {
    $.ajax({
        type: "POST",
        url: '@Url.Action("Method", "Controller")',
        data: {
            clusterId: id,
        },
        success: function (response) {
            if (response.isSdkUpgrading) {
                return true;
            }
            else {
                return false;
            }
        }
    });
}

      

+3


source to share


3 answers


Your logic is directed towards Javascript being a synchronous language. Unfortunately, you fell into the same trap as me and many others when I first developed in a language like Javascript.

So, your Validation () function sends an AJAX request and then ON SUCCESS (otherwise: HTTP request completed successfully), your ANONYMOUS SUCCESS CALLBACK FUNCTION returns true or false depending on the result. The key here is that the Validation () function does not return true or false in the same way. The Validation function immediately returns "null". It does not wait for the AJAX (HTTP) request to complete.

The fix is ​​that you need:

Option # 1: Skip checking the 2nd parameter called the callback, which is a function. Then you set the AJAX property for a successful parameter callback like this:



var callback = function(response){
    if(response.isSdkUpgrading)Error("error msg");
    else proceedToNextValidation();
}

function Validation(id, callback){
    $.ajax({
        type: "POST",
        url: '@Url.Action("Method", "Controller")',
        data: {clusterId: id},
        success: callback
    });
}

      

Option # 2: Move your logic that should wait for the AJAX (HTTP) request to complete into your anonymous callback:

function Validation(id){
    $.ajax({
        type: "POST",
        url: '@Url.Action("Method", "Controller")',
        data: {clusterId: id,},
        success: function(response){
            if(response.isSdkUpgrading)Error("error msg");
            else proceedToNextValidation();
        }
    });
}

      

Hope it helps.

+4


source


Ideally, you should place your code inside a callback ...



function validateUpgradateStatus()
{
  var target = $("#selectcluster").data("ejDropDownList");
  var clusterId = target.model.itemValue;
  Validation(clusterId);
}
function Validation(id) {
   $.ajax({
     type: "POST",
     url: '@Url.Action("Method", "Controller")',
     data: {
        clusterId: id,
    },
    success: function (response) {
        if (response.isSdkUpgrading) {
             Error("error msg");
        }
        else {
            proceedToNextValidation();
        }
    }
 });
}

      

+2


source


if(!isValidated)
     Error("error msg");
    else
        proceedToNextValidation();

      

If a! not confirmed, then you will get an error, Else continueToNextValidation ();

-1


source







All Articles