title: what happens when I read/write the same file?
body: <p>I'm learning about NodeJS's event model.</p>
  
  <p>I'm confused about the output.</p>
  
  <p>the code simply write some text to a file and read it back.
  the execution order seems out of my control.</p>
  
  <pre class="lang-js prettyprint-override"><code>var fs = require('fs');  
  var ws = fs.createWriteStream("my_file.txt");  
  ws.once('open', function(fd) {  
    console.log( 'ws_open' );  
    ws.write("My first row\n");  
    ws.write("My second row\n");  
    ws.write("My third row\n");  
    ws.destroySoon();  
  });  
  
  ws.once('close', function() {  
    console.log( 'ws_close');  
  });  
  
  var rs = fs.createReadStream("my_file.txt");  
  rs.setEncoding('utf8');  
  rs.once('open', function(fd) {  
    console.log( 'rs_open' );  
  });  
  rs.once('close', function() {  
    console.log( 'rs_close');  
  });  
  
  rs.once('data', function(data) {  
    console.log( 'rs_data' );  
    console.log( data );  
  });  
  
  rs.once('error', function(exception) {  
    console.log( 'rs_exception' );  
    console.log( exception );  
  });  
  </code></pre>
  
  <p>Here is the output.</p>
  
  <blockquote>
    <p>rs_open<br>
    ws_open<br>
    rs_close<br>
    ws_close</p>
  </blockquote>
  
  <p>Sometimes, it becomes</p>
  
  <blockquote>
    <p>ws_open<br>
    rs_open<br>
    rs_data<br>
    My first row  </p>
    
    <p>rs_close<br>
    ws_close  </p>
  </blockquote>
  
  <p>Can anyone give me some explanation?</p>
  
  <p>Thank in advance.</p>