Last active
September 19, 2016 18:32
-
-
Save tjunghans/9c3c03fc37dfa8f5a49e2db56fd412d9 to your computer and use it in GitHub Desktop.
Reading directory contents using node
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
const fs = require("fs"); | |
const path = require("path"); | |
const PATH = "." | |
function listDirs(files) { | |
files.forEach(file => { | |
fs.stat(path.join(PATH, file), (err, stats) => { | |
if (stats.isDirectory()) { | |
console.log(file); | |
} | |
}); | |
}); | |
} | |
function getFiles(path) { | |
return new Promise((resolve, reject) => { | |
fs.readdir(path, { | |
encoding: "utf-8" | |
}, (err, files) => { | |
if (err) { | |
reject(err); | |
} else { | |
resolve(files); | |
} | |
}); | |
}); | |
} | |
getFiles(PATH).then(listDirs); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment