Switch from XMLHttpRequest to fetch API

This commit is contained in:
Matthias Schiffer 2018-12-08 14:41:03 +01:00
parent 69b494f844
commit 84caf3ba5b
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C

View file

@ -46,37 +46,9 @@ export class Listenable<T extends any[]> {
}
}
export function get(url: string): Promise<XMLHttpRequest> {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
const handleError = () => {
if (xhr.readyState !== xhr.DONE) {
reject(new Error('HTTP request ended in state ' + xhr.readyState));
return;
}
reject(new Error('HTTP request returned status ' + xhr.status));
};
xhr.addEventListener('error', handleError);
xhr.addEventListener('load', () => {
if (xhr.readyState !== xhr.DONE || xhr.status !== 200) {
handleError();
return;
}
resolve(xhr);
});
xhr.open('GET', url, true);
xhr.send();
});
}
export async function getJSON(url: string): Promise<any> {
return JSON.parse((await get(url)).responseText);
const res = await window.fetch(url);
return await res.json();
}
export function nextAnimationFrame(): Promise<DOMHighResTimeStamp> {