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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source