Asynchronous call inside a recursive function

I am creating my own desktop application in javascript using CEF , and I have an API to access the file system from CEF. I have a senario in which I need to get the names of all files (there may be directory trees) within a specific directory. I need to get an array of results, I am using jquery promises. I do not understand: when will I resolve the promise to get the final array of results?

/*read all directories under this and get path*/
    var result = [];
    function _processEntries(dirPath) {
        var dirEntry = new NativeFileSystem.DirectoryEntry(dirPath), deferred = new $.Deferred();

        /*async call*/
        dirEntry.createReader().readEntries(
            function (entries) {
                for (var i = 0; i < entries.length; i++) {
                    if (entries[i].isDirectory) {
                        _processEntries(entries[i].fullPath).done(function () {
                            deferred.resolve(result);
                        });
                    } else {
                        result.push(entries[i].fullPath);
                    }
                }
            },
            function (error) {
                console.log("Failed while reading dir:", error);
            }
        );
        return deferred.promise();
    }

// call function

_processEntries("C:/Users/abc").done(function(result){
    console.log("FILES ARRAY:",result);
});

Please suggest any other technique if I do it wrong :)

+4
source share
2 answers

Mmmm, , , , , , , :

function _processEntries(dirPath) {
    var result = [];
    var dirEntry = new NativeFileSystem.DirectoryEntry(dirPath), deferred = new $.Deferred();

    /*async call*/
    dirEntry.createReader().readEntries(
        function (entries) {
            var promises = [];
            for (var i = 0; i < entries.length; i++) {
                if (entries[i].isDirectory) {
                    promises.push(_processEntries(entries[i].fullPath));
                } else {
                    result.push(entries[i].fullPath);
                }
            }

            if(promises.length === 0) {
              deferred.resolve(result);
            } else {
               $.when.apply($,promises).done(function() {
                result.concat(arguments);
                deferred.resolve(result);
              })
            }
        },
        function (error) {
            console.log("Failed while reading dir:", error);
        }
    );
    return deferred.promise();
}
0

. . , promises .

, result _processEntries - .

function getDirectoryEntries(dirPath) {
    // a promise helper function
    var dirEntry = new NativeFileSystem.DirectoryEntry(dirPath),
        deferred = new $.Deferred();
    dirEntry.createReader().readEntries(deferred.resolve, deferred.reject);
    return deferred.promise();
}
function _processEntries(dirPath) {
    return getDirectoryEntries(dirPath).then(function (entries) {
        var promises = [];
        for (var i = 0; i < entries.length; i++) {
             if (entries[i].isDirectory) {
                  promises.push(_processEntries(entries[i].fullPath));
             } else {
                  promises.push($.when(entries[i].fullPath));
             }
        }
        return $.when.apply($, promises).then(function() {
             return [].concat.apply([], arguments);
        });
    });
}

:

_processEntries("C:/Users/abc").then(function(result){
    console.log("FILES ARRAY:",result);
}, function (error) {
    console.log("Failed while reading dir:", error);
});
0

All Articles