Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda