await-mutex
Promised based Mutex for cases where you need to synchronize sequentially the access to a single resource from multiple locations.
A typical use case for a mutex is when multiple asynchronous processes are fired and all of them have to execute another, but the same, asynchronous process as they arrive, one at a time, waiting for previous call (if any) to finish before calling it again.
Examples
file-appender.js
Let you create an object to perform file appends one at a time.
;; { this_filename = filename; this_mutex = ; } async { let unlock = await this_mutex; fs; }
API
;
Mutex
Creates an instance of Mutex (can not be called without new).
let mutex = ;
Mutex.prototype.isLocked
Returns if the mutex instance is (true) or not locked (false).
let unlock = await mutex; console; // prints true
Mutex.prototype.lock: Promise
- Waits until the mutex is unlocked and then locks it.
- It returns an ES2015 standard Promise (this allows the use of async/await) which gets resolved once the mutex is unlocked.
- The promise resolution value is an unlock function that has to be called once the mutex needs to be unlocked.
{ let unlock = await mutex; // wait until mutex is unlocked ; console;} { let unlock = await mutex; // wait until mutex is unlocked console;} let mutex = ; ; // prints SomeFunc inmediately; // waits 3 secs for mutex to be unlocked and then prints SomeOtherFunc
Installation
With npm do:
npm install --save await-mutex
Contributing
Contributors
How to
Take a look to the Contributing Guide