snap-mutex
A promise based mutex micro-library for JavaScript. API is optimized for async functions.
Installation
$ npm install --save snap-mutex
Example
const Mutex = ; const myMutex = ; { let lock; try // Try to get the lock lock = await myMutex; // 60 seconds catch e // Failed to acquire the lock throw 'Mutex locking timeout'; try console; // await doStuff(); finally // Release the lock even if the doStuff() throws an exception lock; } // Now onlyOneAtTime() can be called freelyfor let i = 0; i < 9; i++ ;
Remember to put try ... finally
around the critical section so that the lock gets released in
all cases.