toAsyncIterable
toAsyncIterable<
T>(stream):AsyncGenerator<T,void,unknown>
Defined in: stream-utils.ts:389
Convert a ReadableStream to an async iterable for use in for-await loops.
This async generator wraps a ReadableStream and yields each chunk, allowing you to consume Web Streams using async iteration syntax. Properly handles reader lifecycle and ensures the lock is released. Useful for consuming fetch responses in a more ergonomic way.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”stream
Section titled “stream”ReadableStream<T>
ReadableStream to convert
Returns
Section titled “Returns”AsyncGenerator<T, void, unknown>
Yields
Section titled “Yields”Each chunk from the stream
Example
Section titled “Example”const response = await fetch('/api/stream');for await (const chunk of toAsyncIterable(response.body!)) { console.log('Received chunk:', chunk);}