From f7e1962d305deef48fe632b1033fe4a5a13f7651 Mon Sep 17 00:00:00 2001 From: Giora Guttsait Date: Wed, 19 Jan 2022 16:22:43 +0200 Subject: [PATCH] doc: remove redunant `await` calls from stream docs PR-URL: https://github.com/nodejs/node/pull/41592 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Nitzan Uziely Reviewed-By: Mestery Reviewed-By: Colin Ihrig --- doc/api/stream.md | 63 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/doc/api/stream.md b/doc/api/stream.md index f6cd6a5f05d257..7b6646f3827207 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -1716,7 +1716,7 @@ for await (const item of Readable.from([1, 2, 3, 4]).map((x) => x * 2)) { } // With an asynchronous mapper, making at most 2 queries at a time. const resolver = new Resolver(); -const dnsResults = await Readable.from([ +const dnsResults = Readable.from([ 'nodejs.org', 'openjsf.org', 'www.linuxfoundation.org', @@ -1761,7 +1761,7 @@ for await (const item of Readable.from([1, 2, 3, 4]).filter((x) => x > 2)) { } // With an asynchronous predicate, making at most 2 queries at a time. const resolver = new Resolver(); -const dnsResults = await Readable.from([ +const dnsResults = Readable.from([ 'nodejs.org', 'openjsf.org', 'www.linuxfoundation.org', @@ -1775,6 +1775,65 @@ for await (const result of dnsResults) { } ``` +### `readable.forEach(fn[, options])` + + + +> Stability: 1 - Experimental + +* `fn` {Function|AsyncFunction} a function to call on each item of the stream. + * `data` {any} a chunk of data from the stream. + * `options` {Object} + * `signal` {AbortSignal} aborted if the stream is destroyed allowing to + abort the `fn` call early. +* `options` {Object} + * `concurrency` {number} the maximum concurrent invocation of `fn` to call + on the stream at once. **Default:** `1`. + * `signal` {AbortSignal} allows destroying the stream if the signal is + aborted. +* Returns: {Promise} a promise for when the stream has finished. + +This method allows iterating a stream. For each item in the stream the +`fn` function will be called. If the `fn` function returns a promise - that +promise will be `await`ed. + +This method is different from `for await...of` loops in that it can optionally +process items concurrently. In addition, a `forEach` iteration can only be +stopped by having passed a `signal` option and aborting the related +`AbortController` while `for await...of` can be stopped with `break` or +`return`. In either case the stream will be destroyed. + +This method is different from listening to the [`'data'`][] event in that it +uses the [`readable`][] event in the underlying machinary and can limit the +number of concurrent `fn` calls. + +```mjs +import { Readable } from 'stream'; +import { Resolver } from 'dns/promises'; + +// With a synchronous predicate. +for await (const item of Readable.from([1, 2, 3, 4]).filter((x) => x > 2)) { + console.log(item); // 3, 4 +} +// With an asynchronous predicate, making at most 2 queries at a time. +const resolver = new Resolver(); +const dnsResults = Readable.from([ + 'nodejs.org', + 'openjsf.org', + 'www.linuxfoundation.org', +]).map(async (domain) => { + const { address } = await resolver.resolve4(domain, { ttl: true }); + return address; +}, { concurrency: 2 }); +await dnsResults.forEach((result) => { + // Logs result, similar to `for await (const result of dnsResults)` + console.log(result); +}); +console.log('done'); // Stream has finished +``` + ### Duplex and transform streams #### Class: `stream.Duplex`