Generator function to enumerate files I want to implement a function that enumerates all files in directory. I think the function should


Return a Promise, because underlying readdir call is prefered over readdirSync.
Be a generator function, because checking statSync(...).isFile() might take some time and maybe, I will need only first few files...


So my attempt

import * as fs from 'fs';
import * as path from 'path';

function enumerateFiles(dir: string): Promise&lt;IterableIterator&lt;string&gt;&gt; {
    let result = function* (files: string[]): IterableIterator&lt;string&gt; {
        for (let enty of files) {
            let fullpath = path.join(dir, enty);

            if (fs.statSync(fullpath).isFile())
                yield fullpath;
        }
    };

    return new Promise&lt;IterableIterator&lt;string&gt;&gt;((resolve, reject) =&gt; {
        fs.readdir(dir, (err, files) =&gt; {
            if (err)
                reject(err);
            else
                resolve(result(files));
        });
    });
}


And usage

for (let file of await enumerateFiles(String.raw`C:\Windows\Web\Screen`))
    console.log(file);

// C:\Windows\Web\Screen\img100.jpg
// C:\Windows\Web\Screen\img101.jpg
// ...


What do you think? I'm really not sure about combining Promises with generator functions. Especially in this order...
for (let file of await enumerateFiles(String.raw`C:\Windows\Web\Screen`))


The await looks like a leaky abstraction. Could your generator naturally behave synchronously?  A quote from Exploring ES6


  22.1.5 Use case: receiving asynchronous data 
  
  Generators can receive input from next() via yield. That means that you can wake up a generator whenever new data arrives asynchronously and to the generator it feels like it receives the data synchronously.