doc: update stream.reduce concurrency note#47166
Merged
nodejs-github-bot merged 1 commit intonodejs:mainfrom Mar 24, 2023
Merged
doc: update stream.reduce concurrency note#47166nodejs-github-bot merged 1 commit intonodejs:mainfrom
stream.reduce concurrency note#47166nodejs-github-bot merged 1 commit intonodejs:mainfrom
Conversation
2a31082 to
4b92bfd
Compare
2ac4139 to
487f5db
Compare
487f5db to
9627c88
Compare
rluvaton
commented
Mar 20, 2023
Comment on lines
2508
to
+2523
| ```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); | ||
| ``` |
Member
Author
There was a problem hiding this comment.
Updated this example as well so the refactor can be more obvious
Collaborator
Member
Author
|
Hey @VoltrexKeyva , can you please rereview so it can be merged? |
VoltrexKeyva
approved these changes
Mar 23, 2023
Member
Author
|
Thanks @VoltrexKeyva , can you merge it? |
Collaborator
|
Landed in c588145 |
RafaelGSS
pushed a commit
that referenced
this pull request
Apr 5, 2023
PR-URL: #47166 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Merged
RafaelGSS
pushed a commit
that referenced
this pull request
Apr 6, 2023
PR-URL: #47166 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
RafaelGSS
pushed a commit
that referenced
this pull request
Apr 7, 2023
PR-URL: #47166 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
danielleadams
pushed a commit
that referenced
this pull request
Jul 6, 2023
PR-URL: #47166 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
At first, when I read that note I thought I should do this:
This did not work and does not make any sense as the map items are emitted in the order that got from the readable stream.
So I thought of changing to note to recommend implementing reduce using
.forEachwith concurrency but then I understand what @benjamingr meant when he wrote that note - because the only way you will need concurrency is if you do some async operation (because sync block the loop - bla bla) and that operation can be parallelized so I updated the note with an example@benjamingr, hope this is what you meant 😅