title: Asynchronous file appends
body: <p>In trying to learn node.js/socket.io I have been messing around with creating a simple file uploader that takes data chunks from a client browser and reassembles on server side. </p>
  
  <p>The socket.io event for receiving a chunk looks as follows: </p>
  
  <pre><code>socket.on('sendChunk', function (data) {
      fs.appendFile(path + fileName, data.data, function (err) {
        if (err) 
         throw err;
        console.log(data.sequence + ' - The data was appended to file ' + fileName);
      });
    });
  </code></pre>
  
  <p>The issue is that data chunks aren't necessarily appended in the order they were received due to the async calls. Typical console output looks something like this: </p>
  
  <ul>
  <li>1 - The data was appended to file testfile.txt</li>
  <li>3 - The data was appended to file testfile.txt</li>
  <li>4 - The data was appended to file testfile.txt</li>
  <li>2 - The data was appended to file testfile.txt</li>
  </ul>
  
  <p>My question is, what is the proper way to implement this functionality in a non-blocking way but enforce sequence. I've looked at libraries like <a href="https://github.com/caolan/async" rel="nofollow">async</a>, but really want to be able to process each as it comes in rather than creating a series and run once all file chunks are in. I am still trying to wrap my mind around all this event driven flow, so any pointers are great. </p>