diff --git a/scripts/Utils/fetchWithProgress.ts b/scripts/Utils/fetchWithProgress.ts index 224a61b3..ed31094b 100644 --- a/scripts/Utils/fetchWithProgress.ts +++ b/scripts/Utils/fetchWithProgress.ts @@ -1,23 +1,20 @@ export function fetchWithProgress(url, options: RequestInit, onProgress: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null): Promise { return new Promise((resolve, reject) => { let xhr = new XMLHttpRequest() + + xhr.addEventListener("load", e => resolve(xhr.responseText)) + xhr.addEventListener("error", reject) + + if(onProgress && xhr.upload) { + xhr.upload.addEventListener("progress", onProgress) + } + xhr.open(options.method || "GET", url) for(let k in options.headers || {}) { xhr.setRequestHeader(k, options.headers[k]) } - xhr.onload = e => resolve(xhr.responseText) - xhr.onerror = reject - - if(onProgress) { - xhr.addEventListener("progress", onProgress) - - if(xhr.upload) { - xhr.upload.onprogress = onProgress - } - } - xhr.send(options.body) }) }