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?