Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!