Iterating file directory with promises and recursion
<p>I know I'm returning early in the following function, how can I chain the recursive promises to my result?</p>

<p>My goal is to get an array of list of files in the directory and all of it's subdirectories. Array is single dimension, I'm using <a href="http://www.w3schools.com/jsref/jsref_concat_array.asp" rel="nofollow">concat</a> in this example.</p>

<pre><code>function iterate(body) {
    return new Promise(function(resolve, reject){
        var list = [];
        fs.readdir(body.path, function(error, list){
            list.forEach(function(file){
                file = path.resolve(body.path, file);
                fs.stat(file, function(error, stat){
                    console.log(file, stat.isDirectory());
                    if(stat.isDirectory()) {
                        return iterate({path: file})
                            .then(function(result){
                                list.concat(result);
                            })
                            .catch(reject);
                    } else {
                        list.push(file);
                    }
                })
            });
            resolve(list);
        });
    });
};
</code></pre>