Skip to content

Instantly share code, notes, and snippets.

@Aleksey-Danchin
Created April 15, 2022 08:24
Show Gist options
  • Save Aleksey-Danchin/d69a8ded29d67643e067ba9edf5103d8 to your computer and use it in GitHub Desktop.
Save Aleksey-Danchin/d69a8ded29d67643e067ba9edf5103d8 to your computer and use it in GitHub Desktop.
Декоратор для отслеживания прогресса загрузки ответа.
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