Trees of Promises in ES6
This blog post shows how to handle trees of ES6 Promises , via an example where the contents of a directory are listed asynchronously. The challenge We’d like to implement a Promise-based asynchronous function listFile(dir) whose result is an Array with the paths of the files in the directory dir . As an example, consider the following invocation: listFiles('/tmp/dir') .then(files => { console.log(files.join('\n')); }); One possible output is: /tmp/dir/bar.txt /tmp/dir/foo.txt /tmp/dir/subdir/baz.txt The solution For our solution, we create Promise-based versions of the two Node.js functions fs.readdir() and fs.stat() : readdirAsync(dirpath) : Promise<Array<string>> statAsync(filepath) : Promise<Stats> We do so via the library function denodify : import denodeify from 'denodeify'; import {readdir,stat} from 'fs'; const readdirAsync = denodeify(readdir); const statAsync = d...