diff --git a/app/scripts/backend/common.ts b/app/scripts/backend/common.ts index a7671a6a..0108bb56 100644 --- a/app/scripts/backend/common.ts +++ b/app/scripts/backend/common.ts @@ -27,18 +27,14 @@ export async function korpRequest( // Send request const response = await fetch(url, request) - let data: KResponse // If progress handler given, parse response data as it comes in - if (options.onProgress && response.body) { - const json = await readIncrementally(response, (json) => { + const json = await readIncrementally(response, (json) => { + if (options.onProgress) { const progress = calcProgress(json) - if (progress) options.onProgress!(progress) - }) - data = JSON.parse(json) - } else { - // If no progress handler, just await and parse the whole response - data = await response.json() - } + if (progress) options.onProgress(progress) + } + }) + const data: KResponse = JSON.parse(json) if ("ERROR" in data) { const { type, value } = data.ERROR as ErrorMessage @@ -48,8 +44,8 @@ export async function korpRequest( return data } -/** Read and handle a JSON HTTP response as it comes in */ -async function readIncrementally(response: Response, handle: (data: string) => void): Promise { +/** Read and handle a HTTP response body as it comes in */ +async function readIncrementally(response: Response, handle: (content: string) => void): Promise { const reader = response.body!.getReader() let content = "" while (true) {