Created
April 15, 2022 08:24
-
-
Save Aleksey-Danchin/d69a8ded29d67643e067ba9edf5103d8 to your computer and use it in GitHub Desktop.
Декоратор для отслеживания прогресса загрузки ответа.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function byProgress(response, progressHandler) { | |
const reader = response.body.getReader(); | |
const chunks = []; | |
let length = 0; | |
while (true) { | |
const { done, value } = await reader.read(); | |
if (done) { | |
break; | |
} | |
chunks.push(value); | |
length += value.length; | |
progressHandler(length); | |
} | |
const buff = new Uint8Array(length); | |
let index = 0; | |
for (const chunk of chunks) { | |
buff.set(chunk, index); | |
index += chunk.length; | |
} | |
return new TextDecoder("utf-8").decode(buff); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment