import { getIterableStream } from "https://deno.land/x/codepoint_iterator@v1.1.1/iterable.ts";
Converts a ReadableStream
into an async iterable. This allows for easier consumption
of stream data using asynchronous iteration, providing a more modern approach to handling streamed data.
Ideally this would already be built into ReadableStream, but it's currently not so this should help tide over til js runtimes support async iterables for ReadableStreams.
Examples
Example 1
Example 1
const responseStream = fetch('https://example.com/data').then(res => res.body);
for await (const chunk of getIterableStream(await responseStream)) {
console.log(new TextDecoder().decode(chunk)); // Assuming the stream is text data
}
Converting a ReadableStream
from a fetch request into an async iterable,
and then asynchronously iterating over each chunk of data, decoding and logging the text content.
Parameters
stream: ReadableStream<T>
The ReadableStream
to be converted into an async iterable. This stream can contain any type of data, typically Uint8Array
for binary data.