title: Cannot compile async code with generators in Typescript
body: <p><strong>Context</strong></p>
  
  <ol>
  <li><p>In my journey into Typescript, I was advised that <em>blocking calls should never be done inside async code</em>.</p></li>
  <li><p>I'm also using generators, since they make directory traversal easy and avoid running out of stack space.</p></li>
  </ol>
  
  <p>However, when I combine async code (in this case: <code>readdir</code>) with generators, the compiler complains that <em>yield can only be used inside generators</em>, leading me to think that the compiler is not able to combine closures, generators and async code, all in one go.</p>
  
  <pre><code>function *yyyymmGenerator(dir: string, props: Props) {
      const fs    = require("fs");
      const yyyy  = props.range.getUTCFullYear().toString();
      const mm    = props.range.getUTCMonth().toString();
      const start = `${yyyy}-${mm}`;
  
      const files = fs.readdir(dir, function(err, files) {
          for (let i = 0; i &lt; files.length; i++) {
              const file: string = files[i];
              if (file.localeCompare(start) &gt;= 0) {
                  const d = `${dir}/${file}`;
                  yield file;
              }
          }
      });
  }
  
  error TS1163: A 'yield' expression is only allowed in a generator body.
  </code></pre>
  
  <p><strong>Questions</strong></p>
  
  <p>What would be the recommended best practice in a situation like this?</p>
  
  <p>Would it be OK if I simply consider everything sync, blocking code but "wrap" the call inside a Promise?</p>