Last active
August 29, 2015 14:04
-
-
Save edygar/ee0945a73c79182367df to your computer and use it in GitHub Desktop.
Get subdirectories asynchronously
This file contains 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
var fs = require('fs'); | |
module.exports = function getSubDirectories( dir ) | |
{ | |
var deferred = new Promise(); | |
var dirs = [], count = 0; | |
fs.readdir(dir, function(err, files) | |
{ | |
if (err) deferred.error(err); | |
if (!files.length) return deferred.resolve([]); | |
files.forEach(function( file ) | |
{ | |
stat(dir, function(err, fileInfo) | |
{ | |
if (err) deferred.error(err); | |
if (fileInfo.isDirectory()) | |
dirs.push(file); | |
count++; | |
if (count === files.length) | |
deferred.resolve(dirs); | |
}) | |
}); | |
}); | |
return dirs; | |
} |
This file contains 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
var Rx = require('rx'), | |
fs = require('fs'), | |
readdir = Rx.Observable.fromNodeCallback(fs.readdir), | |
stat = Rx.Observable.fromNodeCallback(fs.statS); | |
Array.prototype.toObservable = function() | |
{ | |
return Rx.Observable.fromAarray(this); | |
} | |
module.exports = function getSubDirectoriesRx(param) | |
{ | |
return readdir(param) | |
.mapFlat(function(files) | |
{ | |
return files | |
.map(function(file) | |
{ | |
return stat(file) | |
.filter(function(fileInfo) { | |
return fileInfo.isDirectory() | |
}) | |
.map(function() { return file }) | |
}) | |
.toObservable() | |
.mergeAll(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a version that uses only rx for mapping: (coffeescript)