Skip to content

Instantly share code, notes, and snippets.

@wong2
Created October 25, 2023 07:39
Show Gist options
  • Save wong2/6dec43547ff6295d8dd632e896515721 to your computer and use it in GitHub Desktop.
Save wong2/6dec43547ff6295d8dd632e896515721 to your computer and use it in GitHub Desktop.
async function* streamAsyncIterable<T = unknown>(stream: ReadableStream<T>) {
const reader = stream.getReader()
try {
while (true) {
const { done, value } = await reader.read()
if (done) {
return
}
yield value
}
} finally {
reader.releaseLock()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment