Asynchronous, synchronous and delayed Mutex with ES6 and Promises.
Table of contents
This module can then be installed with yarn:
yarn add @jdes/mutex
/*** @class*/const Mutex = ;
/*** @type*/const mutex = ;
// Instantiateconst mutex = ;let name = null;let iterations = 0;// Lock and perform and unlock asynchronouslymutex;// Lock, perform and unlock asynchronously with delay.// This section try to lock every 100ms but the delay accept to be locked every 200ms.const interval =;
Indicate if the mutex is locked
- returns true if the mutex success to lock
Try to lock synchronously the mutex and return true if it success
- returns a resolved Promise
Unlock the mutex
- return a Promise resolved with the unlock callback if the mutex success to lock.
Try to lock the mutex and resolve the promise
delayed(delay: number): Promise
delaythe delay in ms
- return a Promise resolved with tLOCKEDhe unlock callback if the mutex success to lock.
Try to lock the mutex and resolve the promise.
The mutex can be unlocked only in a interval of
The interval is computed when the method is called so you can unlock the mutex before the end of the interval with
invoke an othe
mutex.delay(delay: number) with a lesser delay.
You should have a mutex for every feature and not reuse a mutex for others. It is useful to keep performance on polling functions.
let iterations = 0;const interval =;