-
Notifications
You must be signed in to change notification settings - Fork 56
/
map.js
36 lines (33 loc) · 1.11 KB
/
map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// 遍历目录,找出最大的一个文件-通过Promise.all()和.map()
// https://www.imooc.com/video/16622
const fs = require('fs');
const path = require('path');
const FileSystem = require('./FileSystem');
function findLargest(dir) {
return FileSystem.readDir(dir, 'utf-8')
.then( files => {
return Promise.all( files.map( file => {
return new Promise (resolve => {
fs.stat(path.join(dir, file), (err, stat) => {
if (err) throw err;
if (stat.isDirectory()) {
return resolve({
size: 0
});
}
stat.file = file;
resolve(stat);
});
});
}));
})
.then( stats => {
let biggest = stats.reduce( (memo, stat) => {
if(memo.size < stat.size) {
return stat;
}
return memo;
});
return biggest.file;
})
}