Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
The else block gets executed always before validating the if condition

The else block gets executed always before validating the if condition. The proceedToNextValidation() executed before the Validation(clusterId) gets fully completed its execution. How to wait for the function Validation(clusterId) to be executed fully and then based on its result evaluate the if-else block.

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;
            }
        }
    });
}
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Your logic is aimed towards Javascript being a synchronous language. Unfortunately, you have fallen for the same trap as I and many others have when first developing in a language like Javascript.

So your Validation() function sends an AJAX request, and then ON SUCCESS (aka: the 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 is not returning true or false in the same way. Immediately, the Validation function actually returns 'null'. It doesn't wait for the AJAX (HTTP) request to complete.

The fix here is that you need to:

Option #1: Pass Validation a 2nd parameter called callback, which is a function. You then set the success AJAX property to your parameter callback as so:

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 needs to wait for the AJAX (HTTP) request to complete into your anonymous success callback as so:

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 this helps.

about 4 years ago · Santiago Trujillo Report

0

You should ideally place your code inside the callback then...

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();
        }
    }
 });
}
about 4 years ago · Santiago Trujillo Report

0

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

If ! not Validated then you will get the error, Else proceedToNextValidation();

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!