global: true (по умолчанию)ajaxCatchUp().then(function(data) {
console.log(data);
});
function ajaxCatchUp() {
var promise = $.Deferred();
$(document).on('ajaxComplete', function(event, jqXHR) {
jqXHR.done(promise.resolve).fail(promise.reject);
});
return promise;
}
requestCatchUp(function(data) {
console.log(data);
});
function requestCatchUp(success, error) {
proxyRequest('open', function() {
this.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 400) {
if (success) {
var data = (function() {
try {
return JSON.parse(this.responseText);
} catch (err) {
return this.responseText;
}
}.bind(this))();
success.call(this, data);
}
} else {
if (error) error.call(this);
}
}
};
return this;
});
}
function proxyRequest(method, callback) {
(function(native) {
XMLHttpRequest.prototype[method] = function() {
native.apply(callback.apply(this, arguments), arguments);
};
})(XMLHttpRequest.prototype[method]);
}