@@ -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
@@ -1480,6 +1480,31 @@ async function run() {
14801480run ().catch (console .error );
14811481```
14821482
1483+ ### Readable.from(iterable, [ options] )
1484+
1485+ * ` iterable ` {Iterable} Object implementing the ` Symbol.asyncIterator ` or
1486+ ` Symbol.iterator ` iterable protocol.
1487+ * ` options ` {Object} Options provided to ` new stream.Readable([options]) ` .
1488+ By default, ` Readable.from() ` will set ` options.objectMode ` to ` true ` , unless
1489+ this is explicitly opted out by setting ` options.objectMode ` to ` false ` .
1490+
1491+ A utility method for creating Readable Streams out of iterators.
1492+
1493+ ``` js
1494+ const { Readable } = require (' stream' );
1495+
1496+ async function * generate () {
1497+ yield ' hello' ;
1498+ yield ' streams' ;
1499+ }
1500+
1501+ const readable = Readable .from (generate ());
1502+
1503+ readable .on (' data' , (chunk ) => {
1504+ console .log (chunk);
1505+ });
1506+ ```
1507+
14831508## API for Stream Implementers
14841509
14851510<!-- type=misc-->
@@ -2395,6 +2420,89 @@ primarily for examples and testing, but there are some use cases where
23952420
23962421<!-- type=misc-->
23972422
2423+ ### Streams Compatibility with Async Generators and Async Iterators
2424+
2425+ With the support of async generators and iterators in JavaScript, async
2426+ generators are effectively a first-class language-level stream construct at
2427+ this point.
2428+
2429+ Some common interop cases of using Node.js streams with async generators
2430+ and async iterators are provided below.
2431+
2432+ #### Consuming Readable Streams with Async Iterators
2433+
2434+ ``` js
2435+ (async function () {
2436+ for await (const chunk of readable ) {
2437+ console .log (chunk);
2438+ }
2439+ })();
2440+ ```
2441+
2442+ #### Creating Readable Streams with Async Generators
2443+
2444+ We can construct a Node.js Readable Stream from an asynchronous generator
2445+ using the ` Readable.from ` utility method:
2446+
2447+ ``` js
2448+ const { Readable } = require (' stream' );
2449+
2450+ async function * generate () {
2451+ yield ' a' ;
2452+ yield ' b' ;
2453+ yield ' c' ;
2454+ }
2455+
2456+ const readable = Readable .from (generate ());
2457+
2458+ readable .on (' data' , (chunk ) => {
2459+ console .log (chunk);
2460+ });
2461+ ```
2462+
2463+ #### Piping to Writable Streams from Async Iterators
2464+
2465+ In the scenario of writing to a writeable stream from an async iterator,
2466+ it is important to ensure the correct handling of backpressure and errors.
2467+
2468+ ``` js
2469+ const { once } = require (' events' );
2470+
2471+ const writeable = fs .createWriteStream (' ./file' );
2472+
2473+ (async function () {
2474+ for await (const chunk of iterator ) {
2475+ // Handle backpressure on write
2476+ if (! writeable .write (value))
2477+ await once (writeable, ' drain' );
2478+ }
2479+ writeable .end ();
2480+ // Ensure completion without errors
2481+ await once (writeable, ' finish' );
2482+ })();
2483+ ```
2484+
2485+ In the above, errors on the write stream would be caught and thrown by the two
2486+ ` once ` listeners, since ` once ` will also handle ` 'error' ` events.
2487+
2488+ Alternatively the readable stream could be wrapped with ` Readable.from ` and
2489+ then piped via ` .pipe ` :
2490+
2491+ ``` js
2492+ const { once } = require (' events' );
2493+
2494+ const writeable = fs .createWriteStream (' ./file' );
2495+
2496+ (async function () {
2497+ const readable = Readable .from (iterator);
2498+ readable .pipe (writeable);
2499+ // Ensure completion without errors
2500+ await once (writeable, ' finish' );
2501+ })();
2502+ ```
2503+
2504+ <!-- type=misc-->
2505+
23982506### Compatibility with Older Node.js Versions
23992507
24002508<!-- type=misc-->
@@ -2531,6 +2639,7 @@ contain multi-byte characters.
25312639[ Compatibility ] : #stream_compatibility_with_older_node_js_versions
25322640[ HTTP requests, on the client ] : http.html#http_class_http_clientrequest
25332641[ HTTP responses, on the server ] : http.html#http_class_http_serverresponse
2642+ [ Readable.from ] : #readable.from
25342643[ TCP sockets ] : net.html#net_class_net_socket
25352644[ child process stdin ] : child_process.html#child_process_subprocess_stdin
25362645[ child process stdout and stderr ] : child_process.html#child_process_subprocess_stdout
0 commit comments