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

111
Vistas
AJAX callbacks replace with Promise

The code below works fine without using Promise. But I am also learning Promise. I would like to request if continue using Existing Codes is feasible for the long run as I can very well manage my callbacks or should I try adapting the Promise logic.

Also, using Promise, my code does not work, so help in the NEW CODES WITH PROMISES section will highly be appreciated.

EXISTING CODES

var init = function () {
    getFirstFunction(() => getSecondFunction(() => {

    }));
};

function getFirstFunction(callback) {
$.getAjax('urlGoesHere', function (response) {
    if (response.isSuccessful) {
       //do whatever you want do with your response.data
       if (callback) { callback(); }
      }
    else {}
});
}

function getSecondFunction(callback) {
$.getAjax('listHelper/getSites', function (response) {
    if (response.isSuccessful) {
        //do whatever you want do with your response.data
        if (callback) {
            callback();
        }
    } else {
    }
});
}

AJAX SCRIPT
window.$.getAjax = function (url, callback) {
    $.ajax({
        type: 'GET',
        url: url,
        dataType: "json",
        async: true,
        crossDomain: true,
        cache: false,
        success: function (response) {
            success(response, function () {
                if (callback) {
                    callback(response);
                }
            });
        },
        failure: function (response) {
            callback(response);
        },
        error: function (response) {
            callback(response);
        }
    });
};

NEW CODES WITH PROMISES

function getFirstFunction(callback) {
$.getAjax('urlGoesHere', function (response)
  .then((data) => {
    console.log(data)
    if(callback) callback();
  })
  .catch((error) => {
    console.log(error)
  })
});
}


AJAX SCRIPT
window.$.getAjax = function (url, callback) {
    return new Promise((resolve, reject) => {
    $.ajax({
      .....
      success: function (response) {
            success(response, function () {
                if (callback) {
                    callback(resolve(response));
                }
            });
        }
....
    });
});
};

about 4 years ago · Santiago Trujillo
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