Javascript release resources automatically (like RAII)
===

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.

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).

For example, in C++ I would do something like the following:

    void MyClass::dangerous(void) {
         MyLock lock(&this->mutex);
         ...
         // at the end of this function, lock will be destructed and release this->mutex.
    }

As far as I can tell, JavaScript doesn't provide any RAII functionality. In C, I would use `goto`s to unwind my resource allocation in the event of an error so that I only have one return path from a function.

What are some techniques to achieve a similar effect in Javascript?