My general question is, what methods can I use to ensure that resources are cleaned / freed in Javascript? I am currently taking the C approach (without goto) to find each execution path to return or throw away in my functions and provide cleanup.
My specific example is the following: In Node.js, I use mutexes (via file locks) in member functions (I need a mutual exception because I run multiple instances of the Node.js application and 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); ...
As far as I can tell, JavaScript does not provide any RAII functionality. In C, I would use goto
to unwind resource allocation in the event of an error, so I only have one way to return from the function.
What are some methods to achieve a similar effect in Javascript?
Chris source share