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 llamada o debería intentar adaptar la lógica de la 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)); } }); } .... }); }); };