Async Await Mutex Lock
Mutex locks for async functions with functionality to use keys for separate locks
Usage Instructions
Without Key
import Lock from "async-await-mutex-lock";
let lock = new Lock();
async function test(): Promise<void> {
await lock.acquire();
try {
}
finally {
lock.release();
}
}
With Key
All the keys will have their own separate locks and separate waiting lists. A key can have
any type (eg. string, number, etc. or a custom type allowed by typescript as a Map key)
import Lock from "async-await-mutex-lock";
let lock = new Lock<string>();
async function test() {
await lock.acquire("myKey");
try {
}
finally {
lock.release("myKey");
}
}
async function testTwo() {
await lock.acquire("myKeyTwo");
try {
}
finally {
lock.release("myKeyTwo");
}
}
Checking if a lock is acquired or not
import Lock from "async-await-mutex-lock";
let lock = new Lock();
async function test() {
await lock.acquire();
console.log(lock.isAcquired())
}
isAcquired()
with key
checks for the given key separately.
import Lock from "async-await-mutex-lock";
let lock = new Lock<string>();
async function test() {
await lock.acquire("myKey");
console.log(lock.isAcquired("myKey"))
}
Issues or Bugs
In case of any issues or bugs, please open a pull request here
Credits
This package has been inspired from await-lock
with an added functionality of allowing keys and checking if lock has been acquired or not