@@ -48,6 +48,13 @@ Additionally, this module includes the utility functions
4848[ ` stream.pipeline() ` ] [ ] , [ ` stream.finished() ` ] [ ] and
4949[ ` stream.Readable.from() ` ] [ ] .
5050
51+ ### Streams Promises API
52+
53+ The ` stream/promises ` API provides an alternative set of asynchronous utility
54+ functions for streams that return ` Promise ` objects rather than using
55+ callbacks. The API is accessible via ` require('stream/promises') `
56+ or ` require('stream').promises ` .
57+
5158### Object mode
5259
5360All streams created by Node.js APIs operate exclusively on strings and ` Buffer `
@@ -1597,10 +1604,10 @@ Especially useful in error handling scenarios where a stream is destroyed
15971604prematurely (like an aborted HTTP request), and will not emit ` 'end' `
15981605or ` 'finish' ` .
15991606
1600- The ` finished ` API is promisify-able as well;
1607+ The ` finished ` API provides promise version:
16011608
16021609``` js
1603- const finished = util . promisify ( stream . finished );
1610+ const { finished } = require ( ' stream/promises ' );
16041611
16051612const rs = fs .createReadStream (' archive.tar' );
16061613
@@ -1684,10 +1691,10 @@ pipeline(
16841691);
16851692```
16861693
1687- The ` pipeline ` API is promisify-able as well :
1694+ The ` pipeline ` API provides promise version :
16881695
16891696``` js
1690- const pipeline = util . promisify ( stream . pipeline );
1697+ const { pipeline } = require ( ' stream/promises ' );
16911698
16921699async function run () {
16931700 await pipeline (
@@ -1704,7 +1711,7 @@ run().catch(console.error);
17041711The ` pipeline ` API also supports async generators:
17051712
17061713``` js
1707- const pipeline = util . promisify ( stream . pipeline );
1714+ const { pipeline } = require ( ' stream/promises ' );
17081715const fs = require (' fs' );
17091716
17101717async function run () {
@@ -2927,9 +2934,9 @@ handling of backpressure and errors. [`stream.pipeline()`][] abstracts away
29272934the handling of backpressure and backpressure-related errors:
29282935
29292936``` js
2930- const { pipeline } = require (' stream' );
2931- const util = require (' util' );
29322937const fs = require (' fs' );
2938+ const { pipeline } = require (' stream' );
2939+ const { pipeline: pipelinePromise } = require (' stream/promises' );
29332940
29342941const writable = fs .createWriteStream (' ./file' );
29352942
@@ -2943,7 +2950,6 @@ pipeline(iterator, writable, (err, value) => {
29432950});
29442951
29452952// Promise Pattern
2946- const pipelinePromise = util .promisify (pipeline);
29472953pipelinePromise (iterator, writable)
29482954 .then ((value ) => {
29492955 console .log (value, ' value returned' );
0 commit comments