summaryrefslogtreecommitdiff
path: root/doc/api/stream.md
diff options
context:
space:
mode:
Diffstat (limited to 'doc/api/stream.md')
-rw-r--r--doc/api/stream.md40
1 files changed, 40 insertions, 0 deletions
diff --git a/doc/api/stream.md b/doc/api/stream.md
index bd4573e487..0b7b4bee2b 100644
--- a/doc/api/stream.md
+++ b/doc/api/stream.md
@@ -1889,6 +1889,46 @@ await dnsResults.forEach((result) => {
console.log('done'); // Stream has finished
```
+### `readable.toArray([options])`
+
+<!-- YAML
+added: REPLACEME
+-->
+
+> Stability: 1 - Experimental
+
+* `options` {Object}
+ * `signal` {AbortSignal} allows cancelling the toArray operation if the
+ signal is aborted.
+* Returns: {Promise} a promise containing an array (if the stream is in
+ object mode) or Buffer with the contents of the stream.
+
+This method allows easily obtaining the contents of a stream. If the
+stream is in [object mode][object-mode] an array of its contents is returned.
+If the stream is not in object mode a Buffer containing its data is returned.
+
+As this method reads the entire stream into memory, it negates the benefits of
+streams. It's intended for interoperability and convenience, not as the primary
+way to consume streams.
+
+```mjs
+import { Readable } from 'stream';
+import { Resolver } from 'dns/promises';
+
+await Readable.from([1, 2, 3, 4]).toArray(); // [1, 2, 3, 4]
+
+// Make dns queries concurrently using .map and collect
+// the results into an aray using toArray
+const dnsResults = await Readable.from([
+ 'nodejs.org',
+ 'openjsf.org',
+ 'www.linuxfoundation.org',
+]).map(async (domain) => {
+ const { address } = await resolver.resolve4(domain, { ttl: true });
+ return address;
+}, { concurrency: 2 }).toArray();
+```
+
### Duplex and transform streams
#### Class: `stream.Duplex`