1

Function dirList() should return array of folders inside definded directory. I can't understand how return dirList variable only after the function isDir() is executed for each file.

I guess that I should use Q.all(), but I don't know where I should put it :-(

var fs = require('fs'),
    Q = require('q'),
    readdir = Q.denodeify(fs.readdir);

function isDir(path) {
    return Q.nfcall(fs.stat, __dirname + path)
        .then(function (stats) {
            if (stats.isDirectory()) {
                return true;
            } else {
                return false;
            }
        });
}

function dirList(path) {
    return readdir(__dirname + path).then(function (files) {
        var dirList = files.filter(function (file) {
                return isDir(path + file).then(function (isDir) {
                    return isDir;
                });
            });
        return dirList;
    });
}

dirList('/').done(
    function (data) {
        console.log(data);
    },
    function (err) {
        console.log(err);
    }
);
inetbug
  • 409
  • 6
  • 16

2 Answers2

2

The problem you're experiencing is that Array.prototype.filter doesn't know about promises, so it just sees a truthy value (actually a promise object) and adds the file to the output list. One way to solve the problem follows (there may be a "cleaner" way possible with something like AsyncJS):

'use strict';

var fs = require('fs'),
    Q = require('q'),
    readdir = Q.denodeify(fs.readdir);

function isDir(path) {
    return Q.nfcall(fs.stat, __dirname + path)
        .then(function (stats) {
            return stats.isDirectory();
        });
}

function dirList(path) {
    return readdir(__dirname + path).then(function (files) {
        // here we map the list of files to an array or promises for determining
        // if they are directories
        var dirPromises = files.map(function (file) {
            return isDir(path + file);
        });
        // here is the Q.all you need
        return Q.all(dirPromises)
            // here we translate the array or directory true/false values back to file names
            .then(function(isDir) {
                return files.filter(function(file, index) { return isDir[index]; });
            });
    });
}

dirList('/').done(
    function (data) {
        console.log(data);
    },
    function (err) {
        console.log(err);
    });
squid314
  • 1,394
  • 8
  • 9
1

The Array filter method doesn't work with an asynchronous promise result, it expects a boolean to be returned synchronously.

I would suggest using a function that checks whether a given path is a directory, and returns a promise for the file if that was the case and a promise for nothing otherwise. You then can wait with Q.all() for the results of this for each path, and then to join the "positive" results together, omitting the "negative" ones:

function isDir(path) {
    return Q.nfcall(fs.stat, __dirname + path).invoke("isDirectory");
}

function dirList(path) {
    return readdir(__dirname + path).then(function (files) {
        var maybeDirPromiseList = files.map(function (file) {
            return isDir(path + file).then(function (isDir) {
                return isDir ? [file] : []; // Maybe represented as list
            });
        });
        var maybeDirsPromise = Q.all(maybeDirPromiseList);
        return maybeDirsPromise.then(function(maybeDirList) {
             return [].concat.apply([], maybeDirList);
             // is equivalent to:
             var dirList = [];
             for (var i=0; i<maybeDirList.length; i++)
                 if (maybeDirList[i] && maybeDirList[i].length > 0)
                     dirList.push(maybeDirList[i][0]);
             return dirList;
        });
    });
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375