Created
January 3, 2026 18:59
-
-
Save dartess/8e4410711b548716eb7cc46c835ed7ab to your computer and use it in GitHub Desktop.
fetch with progress
This file contains hidden or 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
| // (c) @rdvornov | |
| const response = await fetch('... url goes here ...'); | |
| const progressProxyResponse = new Response(new ReadableStream({ | |
| async start(controller) { | |
| const totalSize = Number(response.headers.get('content-length')); | |
| const reader = response.body.getReader(); | |
| let loadedSize = 0; | |
| while (true) { | |
| const { done, chunk } = await reader.read(); | |
| if (done) { | |
| controller.close(); | |
| break; | |
| } | |
| controller.enqueue(chunk); | |
| loadedSize += chunk.length; | |
| console.log(`Received ${loadedSize} of ${totalSize}`); | |
| } | |
| } | |
| })); | |
| const data = await progressProxyResponse.json(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment