Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive Promises?

I would like to iterate over all files located in the HTML 5 file system and have some event get started once the iteration is complete. Since this is async + promises im having a hard time trying to grasp how it should work.

I am using a angularJS and have created a service to encapsulate html 5 file system specific features.

This is the recursive function:

function walkDirectory(path) {

    fileSystem.getFolderContents(path) //this is the services and it returns a promise containing all files in the current folder or directory
        .then(function(entries) {

            for (var i = 0; i < entries.length; i++) {

                if(entries[i].isFile) {
                    console.log("is File: " + entries[i].name);
                    //do something with file here
                } 
                else if (entries[i].isDirectory) {
                    console.log("is Dir: " + entries[i].name);
                    walkDirectory(entries[i].fullPath);
                }
            }
        });
};

ideally i would like to call the function like so, and have it return a promise which gets executed once all files have been traversed.

walkDirectory("/").then( function() {
  console.log(done);
});

Any tips/ ideas how this can be achieved?

an idea would be to have an array of promises and add a new promise to the array for every file/directory. My attempt:

function walkDirectory(path) {

    var defer= $q.defer();
    var promises = [defer.promise];

    fileSystem.getFolderContents(path)
        .then(function(entries) {

            for (var i = 0; i < entries.length; i++) {

                if(entries[i].isFile) {
                    console.log("is File: " + entries[i].name);
                    //do something with file here
                    defer.resolve();
                    promises.push(defer.promise);
                } 
                else if (entries[i].isDirectory) {
                    console.log("is Dir: " + entries[i].name);
                    promises.push(walkDirectory(entries[i].fullPath));
                }
            }
        });

    return $q.all(promises);
};

walkDirectory("/").then(function() {
    console.log("done");
});

This doesn't seem to be working since done is never displayed in the console.

like image 486
Ivan Bacher Avatar asked Jan 15 '14 17:01

Ivan Bacher


People also ask

How is a Promise used in a recursive function?

This is the function that will be called recursively. Set the starting record location for the query based on the ExclusiveStartKey. Call the DynamoDB scan function. The promise() method “promisifies” the scan function to return a Javascript Promise.

What is recursion example?

Recursion is the process of defining a problem (or the solution to a problem) in terms of (a simpler version of) itself. For example, we can define the operation "find your way home" as: If you are at home, stop moving. Take one step toward home.

What are the 3 states of a JavaScript Promise?

A Promise is in one of these states: pending: initial state, neither fulfilled nor rejected. fulfilled: meaning that the operation was completed successfully. rejected: meaning that the operation failed.

How many types of promises are there in JavaScript?

Promises in JavaScript There are 3 states of the Promise object: Pending: Initial State, before the Promise succeeds or fails. Resolved: Completed Promise. Rejected: Failed Promise.


1 Answers

You're returning the array before you populate it.

Instead, you need to return $q.all(promises) within the then() callback, and return the outer promise:

return fileSystem.getFolderContents(path).then(function(entries) {
    return $q.all(entries.map(function(e) {
        if (e.isFile) {
            // Do something
            return null;  // Don't wait for anything
        } else {
            // Do something
            return walkDirectory(e.fullPath);
        }
    }));
});
like image 52
SLaks Avatar answered Oct 24 '22 05:10

SLaks