diff options
author | rickyes <ives199511@gmail.com> | 2020-06-28 11:29:01 +0300 |
---|---|---|
committer | Robert Nagy <ronagy@icloud.com> | 2020-07-09 10:12:19 +0300 |
commit | 527e2147afe43c7b19fd140ebd048c896705da7f (patch) | |
tree | a99175556e04aba1361e2680bf2b991e6ac77fe3 /doc/api/stream.md | |
parent | 6ae1b9c457444fdfa5e30d9142bb8cdccf35f8ee (diff) |
stream: add promises version to utility functions
PR-URL: https://github.com/nodejs/node/pull/33991
Fixes: https://github.com/nodejs/node/issues/33582
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Diffstat (limited to 'doc/api/stream.md')
-rw-r--r-- | doc/api/stream.md | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/doc/api/stream.md b/doc/api/stream.md index 060cb628443..361958a11a6 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -48,6 +48,13 @@ Additionally, this module includes the utility functions [`stream.pipeline()`][], [`stream.finished()`][] and [`stream.Readable.from()`][]. +### Streams Promises API + +The `stream/promises` API provides an alternative set of asynchronous utility +functions for streams that return `Promise` objects rather than using +callbacks. The API is accessible via `require('stream/promises')` +or `require('stream').promises`. + ### Object mode All 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 prematurely (like an aborted HTTP request), and will not emit `'end'` or `'finish'`. -The `finished` API is promisify-able as well; +The `finished` API provides promise version: ```js -const finished = util.promisify(stream.finished); +const { finished } = require('stream/promises'); const rs = fs.createReadStream('archive.tar'); @@ -1684,10 +1691,10 @@ pipeline( ); ``` -The `pipeline` API is promisify-able as well: +The `pipeline` API provides promise version: ```js -const pipeline = util.promisify(stream.pipeline); +const { pipeline } = require('stream/promises'); async function run() { await pipeline( @@ -1704,7 +1711,7 @@ run().catch(console.error); The `pipeline` API also supports async generators: ```js -const pipeline = util.promisify(stream.pipeline); +const { pipeline } = require('stream/promises'); const fs = require('fs'); async function run() { @@ -2927,9 +2934,9 @@ handling of backpressure and errors. [`stream.pipeline()`][] abstracts away the handling of backpressure and backpressure-related errors: ```js -const { pipeline } = require('stream'); -const util = require('util'); const fs = require('fs'); +const { pipeline } = require('stream'); +const { pipeline: pipelinePromise } = require('stream/promises'); const writable = fs.createWriteStream('./file'); @@ -2943,7 +2950,6 @@ pipeline(iterator, writable, (err, value) => { }); // Promise Pattern -const pipelinePromise = util.promisify(pipeline); pipelinePromise(iterator, writable) .then((value) => { console.log(value, 'value returned'); |