title: Javascript release resources automatically (like RAII)
body: <p>My general question is what techniques can I use to ensure that resources are cleaned up/released in Javascript? Currently, I am taking the C (without goto) approach of finding every execution path to a return or exception in my functions and ensuring clean up occurs.</p>
  
  <p>My specific example is this: In Node.js I am using mutexes (through file locks) in object member functions (I need the mutual exclusion, because I run multiple instances of the Node.js application and have race conditions when different instances interact with the file system).</p>
  
  <p>For example, in C++ I would do something like the following:</p>
  
  <pre><code>void MyClass::dangerous(void) {
       MyLock lock(&amp;this-&gt;mutex);
       ...
       // at the end of this function, lock will be destructed and release this-&gt;mutex.
  }
  </code></pre>
  
  <p>As far as I can tell, JavaScript doesn't provide any RAII functionality. In C, I would use <code>goto</code>s to unwind my resource allocation in the event of an error so that I only have one return path from a function.</p>
  
  <p>What are some techniques to achieve a similar effect in Javascript?</p>