Skip to content

Commit 0e33ce4

Browse files
committed
Section 24: Promise.all-Based Solution
1 parent e6570b6 commit 0e33ce4

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

  • 24-Create-Node-JS-Command-Line-Tools/02-list

24-Create-Node-JS-Command-Line-Tools/02-list/index.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@ const util = require('util')
88
// const lstat = util.promisify(fs.lstat)
99

1010
// Method #3
11-
// const { lstat } = fs.promises
11+
const { lstat } = fs.promises
1212

1313
fs.readdir(process.cwd(), async (err, filenames) => {
1414
if (err) {
1515
console.log(err)
1616
}
1717

18-
for (let filename of filenames) {
19-
try {
20-
const stats = await lstat(filename)
18+
const statPromises = filenames.map((filename) => {
19+
return lstat(filename)
20+
})
2121

22-
console.log(filename, stats.isFile())
23-
} catch (err) {
24-
console.log(err)
25-
}
22+
const allStats = await Promise.all(statPromises)
23+
24+
for(let stats of allStats) {
25+
const index = allStats.indexOf(stats)
26+
27+
console.log(filenames[index], stats.isFile());
2628
}
2729
})
2830

0 commit comments

Comments
 (0)