Can Multiple fs.write to append to the same file guarantee the order of execution?
<p>Assume we have such a program:</p>

<pre><code>// imagine the string1 to string1000 are very long strings, which will take a while to be written to file system
var arr = ["string1",...,"string1000"]; 
for (let i = 1; i &lt; 1000; i++) {
  fs.write("./same/path/file.txt", arr[i], {flag: "a"}});
}
</code></pre>

<p>My question is, <code>will string1 to string1000 be gurantted to append to the same file in order?</code></p>

<p>Since fs.write is async function, I am not sure how each call to fs.write() is really executed. I assume the call to the function for each string should be put somewhere in <code>another thread</code> (like a <code>callstack</code>?) and once the previous call is done the next call can be executed.</p>

<p>I'm not really sure if my understanding is accurate.</p>

<p><strong>Edit 1</strong></p>

<p>As in comments and answers, I see <code>fs.write</code> is not safe for multiple write to same file without waiting for <code>callback</code>. But what about writestream?</p>

<p>If I use the following code, would it guarantee the order of writing?</p>

<pre><code>// imagine the string1 to string1000 are very long strings, which will take a while to be written to file system
var arr = ["string1",...,"string1000"]; 
var fileStream = fs.createWriteFileStream("./same/path/file.txt",  { "flags": "a+" });
for (let i = 1; i &lt; 1000; i++) {
  fileStream.write(arr[i]);
}
fileStream.on("error", () =&gt; {// do something});
fileStream.on("finish", () =&gt; {// do something});
fileStream.end();
</code></pre>

<p>Any comments or corrections will be helpful! Thanks!</p>