Created
February 11, 2016 09:21
-
-
Save mrtnbroder/1dbd85018e807329fe9c to your computer and use it in GitHub Desktop.
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
| walk([dir, []]) | |
| .then(all => console.log('all', all)) | |
| .catch(err => console.error('err', err)) | |
| function readdir(directory) { | |
| return new Promise((accept, reject) => { | |
| fs.readdir(directory, (err, files) => { | |
| if (err) reject(err) | |
| accept(files) | |
| }) | |
| }) | |
| } | |
| function stat(file, results) { | |
| return new Promise((accept, reject) => { | |
| fs.stat(file, (err, stats) => { | |
| if (err || stats && !stats.isDirectory()) { | |
| results.push(file) | |
| return reject(err) | |
| } | |
| return accept([file, results]) | |
| }) | |
| }) | |
| } | |
| function walk(args) { | |
| const dir = args[0] | |
| const results = args[1] | |
| return readdir(dir) | |
| .then((files) => { | |
| const filesP = files.map(f => { | |
| const file = path.resolve(dir, f) | |
| return stat(file, results) | |
| .then(walk) | |
| .catch(x => x) | |
| }) | |
| return Promise.all(filesP) | |
| }) | |
| .then(() => results) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment