Named mutex locks for async functions.
In asynchronous JavaScript program code, multiple async functions can operate on the same data. Named mutexes can be used to protect access to shared resources.
TypeScript:
import {MutexRealm} from "async-named-mutex";
const mutexRealm = new MutexRealm<string>();
async function processResource (resourceId: string) {
const mutex = mutexRealm.createMutex(resourceId);
try {
await mutex.acquire();
// ... process resource ...
} finally {
mutex.release();
}
}
JavaScript:
import {MutexRealm} from "async-named-mutex";
const mutexRealm = new MutexRealm();
async function processResource (resourceId) {
const mutex = mutexRealm.createMutex(resourceId);
try {
await mutex.acquire();
// ... process resource ...
} finally {
mutex.release();
}
}
Any value which is a valid Map index
(string
, number
, object
, ...) can be used as a name (aka key) for a mutex.
NPM package: async-named-mutex