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

139
Vistas
Las devoluciones de llamada de AJAX se reemplazan con Promise

El siguiente código funciona bien sin usar Promise. Pero también estoy aprendiendo Promise . Me gustaría preguntar si continuar usando Existing Codes es factible a largo plazo, ya que puedo administrar muy bien mis devoluciones de llamadas o si debería intentar adaptar la lógica de Promise .

Además, usando Promise, mi código no funciona, por lo que la ayuda en la sección NEW CODES WITH PROMISES será muy apreciada.

CÓDIGOS EXISTENTES

 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); } }); };

NUEVOS CÓDIGOS CON PROMESAS

 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)); } }); } .... }); }); };
over 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