GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — doc: update stream.reduce concurrency note · nodejs/node@908798a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • stream.md
Expand file treeCollapse file tree
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)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.