@@ -46,8 +46,8 @@ There are four fundamental stream types within Node.js:
4646* [ ` Transform ` ] [ ] - ` Duplex ` streams that can modify or transform the data as it
4747 is written and read (for example, [ ` zlib.createDeflate() ` ] [ ] ).
4848
49- Additionally, this module includes the utility functions [ pipeline] [ ] and
50- [ finished] [ ] .
49+ Additionally, this module includes the utility functions [ pipeline] [ ] ,
50+ [ finished] [ ] and [ Readable.from ] [ ] .
5151
5252### Object Mode
5353
@@ -1445,6 +1445,31 @@ async function run() {
14451445run ().catch (console .error );
14461446```
14471447
1448+ ### Readable.from(iterable, [ options] )
1449+
1450+ * ` iterable ` {Iterable} Object implementing the ` Symbol.asyncIterator ` or
1451+ ` Symbol.iterator ` iterable protocol.
1452+ * ` options ` {Object} Options provided to ` new stream.Readable([options]) ` .
1453+ By default, ` Readable.from() ` will set ` options.objectMode ` to ` true ` , unless
1454+ this is explicitly opted out by setting ` options.objectMode ` to ` false ` .
1455+
1456+ A utility method for creating Readable Streams out of iterators.
1457+
1458+ ``` js
1459+ const { Readable } = require (' stream' );
1460+
1461+ async function * generate () {
1462+ yield ' hello' ;
1463+ yield ' streams' ;
1464+ }
1465+
1466+ const readable = Readable .from (generate ());
1467+
1468+ readable .on (' data' , (chunk ) => {
1469+ console .log (chunk);
1470+ });
1471+ ```
1472+
14481473## API for Stream Implementers
14491474
14501475<!-- type=misc-->
@@ -2368,6 +2393,89 @@ primarily for examples and testing, but there are some use cases where
23682393
23692394<!-- type=misc-->
23702395
2396+ ### Streams Compatibility with Async Generators and Async Iterators
2397+
2398+ With the support of async generators and iterators in JavaScript, async
2399+ generators are effectively a first-class language-level stream construct at
2400+ this point.
2401+
2402+ Some common interop cases of using Node.js streams with async generators
2403+ and async iterators are provided below.
2404+
2405+ #### Consuming Readable Streams with Async Iterators
2406+
2407+ ``` js
2408+ (async function () {
2409+ for await (const chunk of readable ) {
2410+ console .log (chunk);
2411+ }
2412+ })();
2413+ ```
2414+
2415+ #### Creating Readable Streams with Async Generators
2416+
2417+ We can construct a Node.js Readable Stream from an asynchronous generator
2418+ using the ` Readable.from ` utility method:
2419+
2420+ ``` js
2421+ const { Readable } = require (' stream' );
2422+
2423+ async function * generate () {
2424+ yield ' a' ;
2425+ yield ' b' ;
2426+ yield ' c' ;
2427+ }
2428+
2429+ const readable = Readable .from (generate ());
2430+
2431+ readable .on (' data' , (chunk ) => {
2432+ console .log (chunk);
2433+ });
2434+ ```
2435+
2436+ #### Piping to Writable Streams from Async Iterators
2437+
2438+ In the scenario of writing to a writeable stream from an async iterator,
2439+ it is important to ensure the correct handling of backpressure and errors.
2440+
2441+ ``` js
2442+ const { once } = require (' events' );
2443+
2444+ const writeable = fs .createWriteStream (' ./file' );
2445+
2446+ (async function () {
2447+ for await (const chunk of iterator ) {
2448+ // Handle backpressure on write
2449+ if (! writeable .write (value))
2450+ await once (writeable, ' drain' );
2451+ }
2452+ writeable .end ();
2453+ // Ensure completion without errors
2454+ await once (writeable, ' finish' );
2455+ })();
2456+ ```
2457+
2458+ In the above, errors on the write stream would be caught and thrown by the two
2459+ ` once ` listeners, since ` once ` will also handle ` 'error' ` events.
2460+
2461+ Alternatively the readable stream could be wrapped with ` Readable.from ` and
2462+ then piped via ` .pipe ` :
2463+
2464+ ``` js
2465+ const { once } = require (' events' );
2466+
2467+ const writeable = fs .createWriteStream (' ./file' );
2468+
2469+ (async function () {
2470+ const readable = Readable .from (iterator);
2471+ readable .pipe (writeable);
2472+ // Ensure completion without errors
2473+ await once (writeable, ' finish' );
2474+ })();
2475+ ```
2476+
2477+ <!-- type=misc-->
2478+
23712479### Compatibility with Older Node.js Versions
23722480
23732481<!-- type=misc-->
@@ -2504,6 +2612,7 @@ contain multi-byte characters.
25042612[ Compatibility ] : #stream_compatibility_with_older_node_js_versions
25052613[ HTTP requests, on the client ] : http.html#http_class_http_clientrequest
25062614[ HTTP responses, on the server ] : http.html#http_class_http_serverresponse
2615+ [ Readable.from ] : #readable.from
25072616[ TCP sockets ] : net.html#net_class_net_socket
25082617[ child process stdin ] : child_process.html#child_process_subprocess_stdin
25092618[ child process stdout and stderr ] : child_process.html#child_process_subprocess_stdout
0 commit comments