title: TypeScript: async generator
body: <p>I'd like to have a function like this:</p>
  
  <pre><code>export async function* iterateDir(dir: string) {
      let list = await fs.readdir(dir); // fs-promise implementation of readdir
      for (let file of list) {
          yield file;
      }
  }
  </code></pre>
  
  <p>Which I would use like:</p>
  
  <pre><code>for (let file in iterateDir(dir)) {
      processFile(file);
  }
  </code></pre>
  
  <p>This doesn't work because a function cannot be both async and a generator.</p>
  
  <p>How would I structure the code to achieve the same?</p>
  
  <ol>
  <li>If I change the <code>await fs.readdir</code> to callbacks, I assume the outer for..of loop would not wait.</li>
  <li>If I get rid of the generator and the directory is huge, <code>iterateDir()</code> will be slow.</li>
  </ol>
  
  <p>For reference: <a href="https://github.com/tc39/proposal-async-iteration#async-generator-functions" rel="noreferrer">async generator function proposal</a></p>