title: Nodejs `fs.createReadStream` as promise
body: <p>Im trying to get <code>fs.createReadStream</code> working as a promise, so after the entire file has been read, it will be resolved.</p>
  <p>In the case bellow, im pausing the stream, executing the awaitable method and resuming.</p>
  <ol>
  <li>How to make <code>.on('end'...</code> to be be executed in the end.</li>
  <li>if 1. is not possible, why the  `.on('wont be fired', maybe i can use it to resolve the promise.</li>
  </ol>
  <pre><code>function parseFile&lt;T&gt;(filePath: string, row: (x: T) =&gt; void, err: (x) =&gt; void, end: (x) =&gt; void) {
          return new Promise((resolve, reject) =&gt; {
              const stream = fs.createReadStream(filePath);
              stream.on('data', async data =&gt; {
                      try {
                          stream.pause();
                          await row(data);
                      } finally {
                          stream.resume();
                      }
                  })
                  .on('end', (rowCount: number) =&gt; {
                      resolve();// NOT REALLY THE END row(data) is still being called after this
                  })
                  .on('close', () =&gt; {
                      resolve();// NEVER BEING CALLED
                  })
                  .on('error', (rowCount: number) =&gt; {
                      reject();// NEVER GETS HERE, AS EXPECTED
                  })
          })
  }
  </code></pre>
  <p><strong>UPDATE</strong><br />
  Here you can actually test it: <a href="https://stackblitz.com/edit/node-czktjh?file=index.js" rel="nofollow noreferrer">https://stackblitz.com/edit/node-czktjh?file=index.js</a></p>
  <p>run <code>node index.js</code></p>
  <p>The output should be 1000 and not 1</p>
  <p>Thanks</p>