Skip to content Skip to sidebar Skip to footer

Q Promises - Node.js Function For Every Element In The Array

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 fil

Solution 1:

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);

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

functiondirList(path) {
    returnreaddir(__dirname + path).then(function (files) {
        // here we map the list of files to an array or promises for determining// if they are directoriesvar dirPromises = files.map(function (file) {
            returnisDir(path + file);
        });
        // here is the Q.all you needreturn 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);
    });

Solution 2:

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:

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

functiondirList(path) {
    returnreaddir(__dirname + path).then(function (files) {
        var maybeDirPromiseList = files.map(function (file) {
            returnisDir(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;
        });
    });
}

Post a Comment for "Q Promises - Node.js Function For Every Element In The Array"