Skip to content

Commit 2ba28e8

Browse files
committed
promise refactor
1 parent 6e5b5ff commit 2ba28e8

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

projone/5_match.js

+34
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,37 @@ fromDir(__dirname,/\.md$/i,function(filename){
136136
console.log('-- found: ',filename);
137137
});
138138

139+
/* ShaLLaX suggests: don't be afraid of white space — fromDir(filename,filter,callback) is less readable than fromDir(filename, filter, callback)
140+
141+
His version, rewritten to include promises:
142+
143+
var path = require('path'), fs=require('fs');
144+
145+
async function fromDir(startPath, filter, callback){
146+
const stats = await fs.promises.stat(startPath);
147+
148+
if (!stats.isDirectory()){
149+
console.log("no dir ",startPath);
150+
return;
151+
}
152+
153+
var files = await fs.promises.readdir(startPath);
154+
155+
for(var i = 0; i < files.length; i++) {
156+
var filename = path.join(startPath, files[i]);
157+
const curStats = await fs.promises.stat(filename);
158+
159+
if (curStats.isDirectory()) {
160+
fromDir(filename, filter, callback);
161+
}
162+
else if (filter.test(filename)) {
163+
callback(filename);
164+
}
165+
};
166+
};
167+
168+
fromDir(__dirname, /\.md$/i, (filename) => {
169+
console.log(-- found: ${filename});
170+
});
171+
172+
*/
17.7 KB
Loading

0 commit comments

Comments
 (0)