Refactor httpGetter.

This commit is contained in:
Spencer Ofwiti 2021-04-17 14:02:29 +03:00
parent 2116a55549
commit 8c08607a3a

View File

@ -1,16 +1,14 @@
function HttpGetter(): void {} function HttpGetter(): void {}
HttpGetter.prototype.get = filename => new Promise((whohoo, doh) => { HttpGetter.prototype.get = filename => new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest(); fetch(filename).then(response => {
xhr.addEventListener('load', (e) => { if (response.ok) {
if (xhr.status === 200) { resolve(response.json());
whohoo(xhr.responseText); } else {
return; reject(`failed with status ${response.status} : ${response.statusText}`);
} }
doh('failed with status ' + xhr.status + ': ' + xhr.statusText); return;
}); });
xhr.open('GET', filename);
xhr.send();
}); });
export { export {