Skip to content

Instantly share code, notes, and snippets.

@strix
Created November 21, 2018 07:37
Show Gist options
  • Save strix/eecc35c3abbfe109bcec4f727100e072 to your computer and use it in GitHub Desktop.
Save strix/eecc35c3abbfe109bcec4f727100e072 to your computer and use it in GitHub Desktop.
Recursive file crawling with async await
const findFiles = async (startPath) => {
const results = []
const findRecursive = async (recursiveStartPath) => {
const files = await readdir(recursiveStartPath)
for (const file of files) {
const currentFile = `${recursiveStartPath}/${file}`
const stats = await stat(currentFile)
if (stats.isFile()) {
results.push(path.resolve(currentFile))
} else if (stats.isDirectory()) {
await findRecursive(currentFile)
}
}
return results
}
const foundFiles = await findRecursive(startPath)
return foundFiles
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment