Skip to content

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.

T

ReadableStream<T>

ReadableStream to convert

AsyncGenerator<T, void, unknown>

Each chunk from the stream

const response = await fetch('/api/stream');
for await (const chunk of toAsyncIterable(response.body!)) {
console.log('Received chunk:', chunk);
}