title: JavaScript async sleep function somehow leads to silent exiting of program
body: <p>I copied an async sleep function from here <a href="https://stackoverflow.com/a/39914235/7492244">https://stackoverflow.com/a/39914235/7492244</a>
  Then I used it basically in this program. <a href="https://nodejs.org/api/readline.html#example-read-file-stream-line-by-line" rel="nofollow noreferrer">https://nodejs.org/api/readline.html#example-read-file-stream-line-by-line</a></p>
  <p>So my own index.js looks like:</p>
  <pre><code>const fs = require('fs');
  const readline = require(&quot;readline&quot;);
  
  function sleep(ms) {
    return new Promise((resolve) =&gt; setTimeout(resolve, ms));
  }
  
  async function main() {
      const fileStream = fs.createReadStream('input.txt');
      let lineReader = readline.createInterface({
          input: fileStream,
          crlfDelay: Infinity,
      });
      console.log(&quot;Enter sleep&quot;);
      await sleep(1000);
      console.log(&quot;Exit sleep&quot;);
  
      for await (const line of lineReader) {
          console.log(&quot;line: &quot; + line);
      }
      console.log(&quot;DONE&quot;);
  }
  
  main();
  </code></pre>
  <p>I get this mind boggling behaviour that it somehow prints <code>Enter sleep</code> and <code>Exit sleep</code> but not <code>DONE</code>. However it does terminate, and without printing any errors.
  I found out after hours of debugging that it works if I remove the call to <code>sleep</code>. What is wrong with this sleep function?</p>
  <p>Edit: Note: I am specifically just trying to understand why the call to <code>sleep</code> breaks (afaics) the flow of the program. The use case or end goal is not important.</p>