During the abort, I am pushing the aborted object inside a array . When I call request again , I want to first call the aborted requests.
Current code:
$.xhrPool = []
var invokeService = function (method, url, params, data, headers = {},
dataType = "json", contentType = "application/json") {
return new Promise(function (resolve, reject, onCancel) {
var xUrl = url + (!params ? '' :
typeof (params) === "object" ? "?" + $.param(params) : "/" + params)
var xhr = $.ajax({
method: method,
headers: { ...headers, "X-URL-Redirect": xUrl },
dataType: dataType,
contentType: contentType,
url: "/api",
data: typeof (data) === "string" ? data : JSON.stringify(data)
})
if ($.xhrPool.length !== 0 ) {
for (var i = 0; i < $.xhrPool.length; i++) {
if ($.xhrPool[i].readyState === 0) {
$.xhrPool[i] = xhr;
xhr.then(function success(data, status, req) {
console.log("call from insdie")
resolve(data)
}, function error(err, status) {
console.log(status, err.responseText)
reject(err.responseText || "Cannot conect to server");
})
}
}
}
xhr.then(function success(data, status, req) {
resolve(data)
}, function error(err, status) {
console.log(status, err.responseText)
reject(err.responseText || "Cannot conect to server");
})
onCancel(function () {
if (xhr && xhr.abort) {
xhr.abort();
$.xhrPool.add(xhr);
}
else console.log("could not abort request")
});
})
}
So when I call the invoke service again I want the $.xhrPool requests to run , and also update the $.xhrPool with only the aborted requests.