Switch from XMLHttpRequest to fetch API
This commit is contained in:
parent
69b494f844
commit
84caf3ba5b
1 changed files with 2 additions and 30 deletions
|
@ -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> {
|
||||
|
|
Reference in a new issue