Skip to content

Instantly share code, notes, and snippets.

@lcaballero
Created October 14, 2013 22:28
Show Gist options
  • Save lcaballero/6983319 to your computer and use it in GitHub Desktop.
Save lcaballero/6983319 to your computer and use it in GitHub Desktop.
Recursive + async traversal of a directory using Promises and Node
Q = require('q')
fs = require('fs')
_ = require("lodash")
isFile = (name) ->
fs.statSync(name).isFile()
isDirectory = (name) ->
fs.statSync(name).isDirectory()
withDir = (dir) ->
(files) -> _.map(files, (f) -> "#{dir}/#{f}")
read = (dir) ->
Q.nfcall(fs.readdir, dir)
.then(withDir dir)
.then((files) -> Q.all(toPromises files))
toPromises = (files) ->
for f in files
if isFile f then Q(f) else read(f) #.then((m) -> m.concat("d " + f))
read("../../WebUI/node_modules", [])
.then((files) -> _.flatten(files))
.then((r) -> console.log r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment