export async function* streamToAsyncIterable(readableStream) { const reader =…
- User
export async function* streamToAsyncIterable(readableStream) { const reader = readableStream.getReader(); const decoder = new TextDecoder();
while (true) { const { value, done } = await reader.read(); if (done) break; yield decoder.decode(value); } }
Assistant
Your
streamToAsyncIterablefunction is a solid and commonly used pattern for converting aReadableStreaminto an async iterable of decoded text chunks. It works well for streaming text-based responses (like those from the Fetch API or server-sent events), especially when you want to process chunks as they arrive.Here's what your code does:
export async function* streamToAsyncIterable(readableStream) { const reader = readableStream.getReader(); const decoder = new TextDecoder(); while (true) { const { value, done } = await reader.read(); if (done) break; yield decoder.decode(value); } }readableStream.getReader(): Gets a reader to pull chunks from the stream.TextDecoder: Converts binary data (Uint8Array) into strings.yield decoder.decode(value): Yields each decoded chunk asynchronously.
✅ Best Practice Notes
-
Error Handling: Currently, this doesn't handle errors that might occur during reading. You may want to wrap the read loop in a try/catch block if you're working in an environment where errors can be thrown during reading.
-
Final Chunk: If there's trailing bytes not fully forming a character (e.g., partial UTF-8 sequence), calling
.decode()without{ stream: true }could result in replacement characters (``). You can fix this by using:yield decoder.decode(value, { stream: true });And then finally call
decoder.decode()with no arguments after the loop ends to flush any remaining buffered bytes.
🛠️ Improved Version (with error handling + proper decoding)
export async function* streamToAsyncIterable(readableStream) { const reader = readableStream.getReader(); const decoder = new TextDecoder(); let done = false; try { while (!done) { const { value, done: readerDone } = await reader.read(); done = readerDone; if (value) { yield decoder.decode(value, { stream: true }); } } // After the loop, flush any remaining bytes in the decoder yield decoder.decode(); } catch (error) { console.error('Error reading stream:', error); throw error; } finally { reader.releaseLock(); } }
🔁 Usage Example
You can use this generator like so:
const response = await fetch('https://example.com/stream-endpoint'); const stream = streamToAsyncIterable(response.body); for await (const chunk of stream) { console.log(chunk); }This will log each decoded text chunk as it arrives.
Let me know if you'd like to support binary streams too, or convert to something like lines or JSON chunks!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.