Skip to content

Instantly share code, notes, and snippets.

@sabitertan
Created July 29, 2019 00:53
Show Gist options
  • Select an option

  • Save sabitertan/af8abe2f9e738cd7189d2e1cf244e75b to your computer and use it in GitHub Desktop.

Select an option

Save sabitertan/af8abe2f9e738cd7189d2e1cf244e75b to your computer and use it in GitHub Desktop.
window.fetch = (url, info) => {
return new Promise(function (resolve, reject) {
let xhr = new XMLHttpRequest();
xhr.open(info.method || "GET", url);
if (url.endsWith(".wasm"))
xhr.responseType = "arraybuffer";
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve({
headers: xhr.getAllResponseHeaders().split("\r\n"),
json: () => JSON.parse(xhr.response),
ok: true,
arrayBuffer: () => xhr.response,
byteLenght: (new TextEncoder('utf-8').encode(xhr.response)).length
});
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
})
};
WebAssembly.instantiateStreaming = undefined;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment