diff --git a/doc/api/webstreams.md b/doc/api/webstreams.md index a413beb7174fd4..bb3374e1480e73 100644 --- a/doc/api/webstreams.md +++ b/doc/api/webstreams.md @@ -387,6 +387,49 @@ port1.onmessage = ({ data }) => { port2.postMessage(stream, [stream]); ``` +### `ReadableStream.from(iterable)` + + + +* `iterable` {Iterable} Object implementing the `Symbol.asyncIterator` or + `Symbol.iterator` iterable protocol. + +A utility method that creates a new {ReadableStream} from an iterable. + +```mjs +import { ReadableStream } from 'node:stream/web'; + +async function* asyncIterableGenerator() { + yield 'a'; + yield 'b'; + yield 'c'; +} + +const stream = ReadableStream.from(asyncIterableGenerator()); + +for await (const chunk of stream) + console.log(chunk); // Prints 'a', 'b', 'c' +``` + +```cjs +const { ReadableStream } = require('node:stream/web'); + +async function* asyncIterableGenerator() { + yield 'a'; + yield 'b'; + yield 'c'; +} + +(async () => { + const stream = ReadableStream.from(asyncIterableGenerator()); + + for await (const chunk of stream) + console.log(chunk); // Prints 'a', 'b', 'c' +})(); +``` + ### Class: `ReadableStreamDefaultReader`