Sonarr/frontend/src/Utilities/createAjaxRequest.js

31 lines
609 B
JavaScript
Raw Normal View History

2018-01-13 02:01:27 +00:00
import $ from 'jquery';
export default function createAjaxRequest(ajaxOptions) {
const requestXHR = new window.XMLHttpRequest();
let aborted = false;
let complete = false;
function abortRequest() {
if (!complete) {
aborted = true;
requestXHR.abort();
}
}
const request = $.ajax({
xhr: () => requestXHR,
...ajaxOptions
}).then(null, (xhr, textStatus, errorThrown) => {
xhr.aborted = aborted;
return $.Deferred().reject(xhr, textStatus, errorThrown).promise();
}).always(() => {
complete = true;
});
return {
request,
abortRequest
};
}