| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| ```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); | ||
| ``` |
There was a problem hiding this comment.
Updated this example as well so the refactor can be more obvious
Sorry, something went wrong.
There was a problem hiding this comment.
lgtm
Sorry, something went wrong.
Sorry, something went wrong.
|
Hey @VoltrexKeyva , can you please rereview so it can be merged? |
Sorry, something went wrong.
|
Thanks @VoltrexKeyva , can you merge it? |
Sorry, something went wrong.
PR-URL: #47166 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
PR-URL: #47166 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
PR-URL: #47166 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
PR-URL: #47166 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
| Back | FazBrowse Home | New Git URL |
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 .forEach with 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 😅