Skip to content

Instantly share code, notes, and snippets.

@mrtnbroder
Created February 11, 2016 09:21
Show Gist options
  • Select an option

  • Save mrtnbroder/1dbd85018e807329fe9c to your computer and use it in GitHub Desktop.

Select an option

Save mrtnbroder/1dbd85018e807329fe9c to your computer and use it in GitHub Desktop.
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