Created
September 11, 2023 03:24
-
-
Save InfiniteXyy/133453cab1d5331dcafdebebcfe95712 to your computer and use it in GitHub Desktop.
Read a stream with async generator
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
async function* readAllChunks(stream: ReadableStream<Uint8Array>) { | |
const reader = stream.getReader(); | |
const decoder = new TextDecoder("utf-8"); | |
while (true) { | |
const { value, done } = await reader.read(); | |
yield decoder.decode(value); | |
if (done) return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment