Skip to content

Instantly share code, notes, and snippets.

@InfiniteXyy
Created September 11, 2023 03:24
Show Gist options
  • Save InfiniteXyy/133453cab1d5331dcafdebebcfe95712 to your computer and use it in GitHub Desktop.
Save InfiniteXyy/133453cab1d5331dcafdebebcfe95712 to your computer and use it in GitHub Desktop.
Read a stream with async generator
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