Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 908798a

Browse filesBrowse files
rluvatonRafaelGSS
authored andcommitted
doc: update stream.reduce concurrency note
PR-URL: #47166 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
1 parent 8a69929 commit 908798a
Copy full SHA for 908798a

File tree

Expand file treeCollapse file tree

1 file changed

+31
-8
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+31
-8
lines changed
Open diff view settings
Collapse file

‎doc/api/stream.md‎

Copy file name to clipboardExpand all lines: doc/api/stream.md
+31-8Lines changed: 31 additions & 8 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -2499,21 +2499,44 @@ This method calls `fn` on each chunk of the stream in order, passing it the
24992499
result from the calculation on the previous element. It returns a promise for
25002500
the final value of the reduction.
25012501

2502-
The reducer function iterates the stream element-by-element which means that
2503-
there is no `concurrency` parameter or parallelism. To perform a `reduce`
2504-
concurrently, it can be chained to the [`readable.map`][] method.
2505-
25062502
If no `initial` value is supplied the first chunk of the stream is used as the
25072503
initial value. If the stream is empty, the promise is rejected with a
25082504
`TypeError` with the `ERR_INVALID_ARGS` code property.
25092505

25102506
```mjs
25112507
import { Readable } from 'node:stream';
2508+
import { readdir, stat } from 'node:fs/promises';
2509+
import { join } from 'node:path';
25122510

2513-
const ten = await Readable.from([1, 2, 3, 4]).reduce((previous, data) => {
2514-
return previous + data;
2515-
});
2516-
console.log(ten); // 10
2511+
const directoryPath = './src';
2512+
const filesInDir = await readdir(directoryPath);
2513+
2514+
const folderSize = await Readable.from(filesInDir)
2515+
.reduce(async (totalSize, file) => {
2516+
const { size } = await stat(join(directoryPath, file));
2517+
return totalSize + size;
2518+
}, 0);
2519+
2520+
console.log(folderSize);
2521+
```
2522+
2523+
The reducer function iterates the stream element-by-element which means that
2524+
there is no `concurrency` parameter or parallelism. To perform a `reduce`
2525+
concurrently, you can extract the async function to [`readable.map`][] method.
2526+
2527+
```mjs
2528+
import { Readable } from 'node:stream';
2529+
import { readdir, stat } from 'node:fs/promises';
2530+
import { join } from 'node:path';
2531+
2532+
const directoryPath = './src';
2533+
const filesInDir = await readdir(directoryPath);
2534+
2535+
const folderSize = await Readable.from(filesInDir)
2536+
.map((file) => stat(join(directoryPath, file)), { concurrency: 2 })
2537+
.reduce((totalSize, { size }) => totalSize + size, 0);
2538+
2539+
console.log(folderSize);
25172540
```
25182541

25192542
### Duplex and transform streams

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.