What is async-lock?
The async-lock npm package is designed to handle synchronization issues in Node.js applications where asynchronous operations need to be executed in a controlled sequence. It provides a mechanism to ensure that certain sections of code that should not be executed concurrently can be locked to avoid race conditions and other concurrency-related bugs.
What are async-lock's main functionalities?
Locking asynchronous operations
This feature allows you to lock a section of asynchronous operations using a unique key. The lock ensures that only one block of code that uses the same key can execute at a time, preventing race conditions.
const AsyncLock = require('async-lock');
const lock = new AsyncLock();
lock.acquire('key', function(done) {
// async work
setTimeout(function() {
done(null, 'result');
}, 1000);
}, function(err, result) {
console.log(result); // prints 'result' after 1000ms
});
Promise support
Async-lock also supports promises, allowing you to use it in modern asynchronous workflows. This feature simplifies working with asynchronous operations by using promises instead of callbacks.
const AsyncLock = require('async-lock');
const lock = new AsyncLock();
lock.acquire('key', () => {
return new Promise(resolve => {
setTimeout(() => {
resolve('result');
}, 1000);
});
}).then(result => {
console.log(result); // prints 'result' after 1000ms
});
Other packages similar to async-lock
mutexify
Mutexify is another npm package that provides a mutual exclusion lock. It is similar to async-lock but differs in its API and internal implementation. Mutexify uses a simpler queue mechanism and does not support timeouts or promise-based APIs, which makes async-lock more versatile for complex applications.
rwlock
RWLock is a package that provides read and write locks, allowing multiple readers or a single writer. This is different from async-lock, which does not distinguish between read and write operations. RWLock is useful in scenarios where the distinction between read and write operations can optimize performance and resource utilization.
async-lock
Mutex on asynchronous code
Get Started
var AsyncLock = require('async-lock');
var lock = new AsyncLock();
/**
* @param {String|Array} key resource key or keys to lock
* @param {function} fn async function with node.js style
* @param {function} cb (optional) callback function
* @param {Object} opts (optional) options
*/
lock.acquire(key, fn, cb, opts);
Acquire multiple keys
lock.acquire([key1, key2], fn, cb);
Specify timeout
var lock = new AsyncLock({timeout : 5000});
lock.acquire(key, fn, function(err){
// timed out error will be returned here if fn has not return within given time
});
//Specify timeout for one function
lock.acquire(key, fn, cb, {timeout : 5000});
Get running state
// Where there is any running or pending async function
lock.isBusy();
License
(The MIT License)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.