title: Node.js WriteStream doesn't write data to file until closed
body: <p>How can I cause the data to be written to the file as <code>WriteStream.write()</code> is called?</p>
  
  <p>Edit:
  As it turns out, this works when I use the REPL and call the function. However, this doesn't work in my program:</p>
  
  <pre><code>import * as FS from "fs";
  import { LetterGroup } from "./LetterGroup";
  import { Dictionary } from "./Dictionary";
  import { Word } from "./Word";
  import * as OS from "os";
  
  const groups: Array&lt;LetterGroup&gt; = LetterGroup.letterGroupsFromString(FS.readFileSync("./letterGroups.txt").toString());
  const dictionary = Dictionary.create(FS.readFileSync("./dictionary.txt").toString());
  
  const inputStr: string = FS.readFileSync("./input.txt").toString();
  const inputWords = new Array&lt;Word&gt;();
  const fileStream = FS.createWriteStream("./output.txt");
  
  for (const line of inputStr.trim().split(OS.EOL))
  {
      inputWords.push(new Word(line));
  }
  
  function permute(index: number)
  {
      index = Math.floor(index);
  
      if (!(index &gt;= 0 &amp;&amp; index &lt; inputWords.length))
      {
          return;
      }
  
      const word: Word = inputWords[index];
  
      let outputString: string = "";
  
      outputString += `Valid permutations of '${word.wordString}':` + OS.EOL;
  
      console.log(`Testing '${ word.wordString }'...`);
  
      for (const permutation of word.generatePermutations(groups, dictionary, true))
      {
          outputString += permutation.wordString + OS.EOL;
      }
  
      outputString += OS.EOL;
      console.log();
  
      if (!fileStream.write(outputString))
      {
          console.log("Wrote to file too fast! Will resume writing once the system catches up...");
          fileStream.once("drain", () =&gt; permute(index));
          return;
      }
  
      permute(index + 1);
  }
  
  permute(0);
  </code></pre>
  
  <p>It should be noted that <code>word.generatePermutations</code> is an extremely intensive function and often can take several minutes (sometimes over an hour) to return. My issue is that the data it returns should be written to the output file immediately so that the entire program doesn't need to finish running for the results to be read. Hopefully someone can make sense of all this.</p>
  
  <p>To clarify, data written to a <code>WriteStream</code> in the REPL is immediately written to the file, while no data is written to the file in my program until the entire program finishes running.</p>