Authenticate-chat-ui / src /lib /utils /streamToAsyncIterable.ts
coyotte508's picture
coyotte508 HF staff
✨ Save messages in backend (#31)
ad02fa3 unverified
raw
history blame contribute delete
439 Bytes
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of#iterating_over_async_generators
export async function* streamToAsyncIterable(
stream: ReadableStream<Uint8Array>
): AsyncIterableIterator<Uint8Array> {
const reader = stream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) return;
yield value;
}
} finally {
reader.releaseLock();
}
}