1

I currently have a wrapper around jQuery.ajax(), which handles some common error checks and provides a default error callback in case no other is defined.

function ajax(name, method, data, successCallback, errorCallback) {

    $.ajax({url: name, type: method, dataType: "json", data: data, success: json => {

        if (json.success) {

            //processing...

            if (successCallback === undefined)
                return;

            successCallback(json);

        } else {

            //processing...

            if (errorCallback !== undefined)
                errorCallback(json.message);
            else
                error(json.message); //default error callback

        }

    }, error: (jqXHR, textStatus, errorThrown) => {

        error(errorThrown);

    }});

}

I was thinking of changing the method to use promises. Is there anyway I could provide a default error callback for promises, which can be overridden using .catch() and maybe even try catch with await?

Snooze
  • 23
  • 4
  • you may be interested to know that `$.ajax` already returns a viable `Promise` – Bravo Oct 15 '19 at 23:09
  • @Bravo I don't think that solves my problem. I could easily make this use promises, I want a default error callback, which can be overridden. – Snooze Oct 15 '19 at 23:13

0 Answers0