The world needed one more mutex library.
A promise-based mutex micro-library for JavaScript. Its API is designed for async functions.
It is useful, for example, when an Express or Koa web server writes a line to a shared file while handling a request. Only one write operation may be in progress at any given time.
Requires Node.js 22 or later. The package is an ES module and includes TypeScript type definitions.
npm install --save snap-mutex
import Mutex from 'snap-mutex';
const myMutex = new Mutex();
async function onlyOneAtTime() {
let lock;
try {
// Try to acquire the lock
lock = await myMutex.acquire({ timeout: 60000 }); // 60 seconds
} catch {
// Failed to acquire the lock
throw new Error('Timed out after waiting 60 seconds for the lock');
}
try {
console.log('Lock acquired');
// await doStuff();
} finally {
// Release the lock even if doStuff() throws
lock.release();
}
}
// onlyOneAtTime() can now be called concurrently
for (let i = 0; i < 9; i++) {
onlyOneAtTime();
}Wrap the critical section in try ... finally so that the lock is released in all cases.
If the lock isn't acquired within timeout milliseconds, acquire() rejects with Error('Queue timeout').