Wait for the keys to be unlocked and lock them. Don't forget to call AsyncLock.release with the same arguments!
Check whether the given key is locked.
If provided with multiple keys it checks whether any of the keys are locked.
Unlock the given key(s). This method should only be called by the code that previously called AsyncLock.acquire
Wait and lock the specified key.
Perform an action using the lock and then release it again. This method always calls AsyncLock.release but doesn't silence any errors.
The provided keys
are interpreted the same as for AsyncLock.acquire.
Generated using TypeDoc
A namespaced Semaphore for asynchronous operations.
Not providing a key to the methods makes this function as if it were a normal Semaphore.
const lock = new AsyncLock(); async function write(text: string, key: string, delay?: number): Promise<void> { await lock.withLock(async () => { // wait 500ms await new Promise(res => setTimeout(res, 500 + (delay || 0))); console.info(text); }, key); } // these operations will practically start at the same time! // but there will be a 500ms delay between write calls of the same key write("this", "a"); write("is", "a"); write("me", "a", 10); write("cute", "b"); write("not", "b"); write("loyal", "b"); write("dog", "c"); write("very", "c"); write("to", "c"); // Output: // *500ms pass* // this cute dog // *500ms pass* // is not very // *500ms pass* // loyal to me