GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — doc: update `stream.reduce` concurrency note by rluvaton · Pull Request #47166 · nodejs/node
doc: update
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2501,21 +2501,44 @@ This method calls `fn` on each chunk of the stream in order, passing it the | |
| result from the calculation on the previous element. It returns a promise for | ||
| the final value of the reduction. | ||
|
|
||
| The reducer function iterates the stream element-by-element which means that | ||
| there is no `concurrency` parameter or parallelism. To perform a `reduce` | ||
| concurrently, it can be chained to the [`readable.map`][] method. | ||
|
|
||
| If no `initial` value is supplied the first chunk of the stream is used as the | ||
| initial value. If the stream is empty, the promise is rejected with a | ||
| `TypeError` with the `ERR_INVALID_ARGS` code property. | ||
|
|
||
| ```mjs | ||
| import { Readable } from 'node:stream'; | ||
| import { readdir, stat } from 'node:fs/promises'; | ||
| import { join } from 'node:path'; | ||
|
|
||
| const ten = await Readable.from([1, 2, 3, 4]).reduce((previous, data) => { | ||
| return previous + data; | ||
| }); | ||
| console.log(ten); // 10 | ||
| const directoryPath = './src'; | ||
| const filesInDir = await readdir(directoryPath); | ||
|
|
||
| const folderSize = await Readable.from(filesInDir) | ||
| .reduce(async (totalSize, file) => { | ||
| const { size } = await stat(join(directoryPath, file)); | ||
| return totalSize + size; | ||
| }, 0); | ||
|
|
||
| console.log(folderSize); | ||
| ``` | ||
|
Comment on lines
2508
to
+2523
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated this example as well so the refactor can be more obvious
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions |
||
|
|
||
| The reducer function iterates the stream element-by-element which means that | ||
| there is no `concurrency` parameter or parallelism. To perform a `reduce` | ||
| concurrently, you can extract the async function to [`readable.map`][] method. | ||
|
|
||
| ```mjs | ||
| import { Readable } from 'node:stream'; | ||
| import { readdir, stat } from 'node:fs/promises'; | ||
| import { join } from 'node:path'; | ||
|
|
||
| const directoryPath = './src'; | ||
| const filesInDir = await readdir(directoryPath); | ||
|
|
||
| const folderSize = await Readable.from(filesInDir) | ||
| .map((file) => stat(join(directoryPath, file)), { concurrency: 2 }) | ||
| .reduce((totalSize, { size }) => totalSize + size, 0); | ||
|
|
||
| console.log(folderSize); | ||
| ``` | ||
|
|
||
| ### Duplex and transform streams | ||
|
|
||