|
|
@@ -41,7 +41,9 @@ this.superFetch = this.superFetch || (() => {
|
|
|
superFetch.hostFetch = async url => {
|
|
|
const xhrPromise = new Promise((resolve, reject) => {
|
|
|
const xhrRequest = new XMLHttpRequest();
|
|
|
- let resolveResponse, rejectResponse;
|
|
|
+ let receivedResponse, receivedError;
|
|
|
+ let resolveResponse = response => receivedResponse = response;
|
|
|
+ let rejectResponse = error => receivedError = error;
|
|
|
xhrRequest.withCredentials = true;
|
|
|
xhrRequest.responseType = "arraybuffer";
|
|
|
xhrRequest.onerror = event => reject(new Error(event.details));
|
|
|
@@ -49,16 +51,26 @@ this.superFetch = this.superFetch || (() => {
|
|
|
if (xhrRequest.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
|
|
|
const headers = new Map();
|
|
|
headers.set("content-type", xhrRequest.getResponseHeader("Content-Type"));
|
|
|
- resolve({ headers, status: xhrRequest.status, arrayBuffer: () => new Promise((resolve, reject) => [resolveResponse, rejectResponse] = [resolve, reject]) });
|
|
|
+ resolve({
|
|
|
+ headers, status: xhrRequest.status, arrayBuffer: () => new Promise((resolve, reject) => {
|
|
|
+ if (receivedError) {
|
|
|
+ reject(receivedError);
|
|
|
+ } else if (receivedResponse) {
|
|
|
+ resolve(receivedResponse);
|
|
|
+ } else {
|
|
|
+ [resolveResponse, rejectResponse] = [resolve, reject];
|
|
|
+ }
|
|
|
+ })
|
|
|
+ });
|
|
|
}
|
|
|
if (xhrRequest.readyState == XMLHttpRequest.DONE) {
|
|
|
- setTimeout(() => resolveResponse(xhrRequest.response), 1);
|
|
|
+ resolveResponse(xhrRequest.response);
|
|
|
}
|
|
|
};
|
|
|
xhrRequest.onerror = error => {
|
|
|
reject(error);
|
|
|
if (rejectResponse) {
|
|
|
- setTimeout(() => rejectResponse(error), 1);
|
|
|
+ rejectResponse(error);
|
|
|
}
|
|
|
};
|
|
|
xhrRequest.open("GET", url, true);
|