333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# async-lock
 
Lock on asynchronous code
 
[![Build Status](https://travis-ci.org/rogierschouten/async-lock.svg?branch=master)](https://travis-ci.org/rogierschouten/async-lock)
 
* ES6 promise supported
* Multiple keys lock supported
* Timeout supported
* Pending task limit supported
* Domain reentrant supported
* 100% code coverage
 
## Disclaimer
 
I did not create this package, and I will not add any features to it myself. I was granted the ownership because it was no longer being
maintained, and I volunteered to fix a bug.
 
If you have a new feature you would like to have incorporated, please send me a PR and I will be happy to work with you and get it merged.
For any bugs, PRs are most welcome but when possible I will try to get them resolved as soon as possible.
 
## Why do you need locking on single threaded nodejs?
 
Nodejs is single threaded, and the code execution never gets interrupted inside an event loop, so locking is unnecessary? This is true ONLY IF your critical section can be executed inside a single event loop.
However, if you have any async code inside your critical section (it can be simply triggered by any I/O operation, or timer), your critical logic will across multiple event loops, therefore it's not concurrency safe!
 
Consider the following code
```js
redis.get('key', function(err, value) {
    redis.set('key', value * 2);
});
```
The above code simply multiply a redis key by 2.
However, if two users run concurrently, the execution order may like this
```
user1: redis.get('key') -> 1
user2: redis.get('key') -> 1
user1: redis.set('key', 1 x 2) -> 2
user2: redis.set('key', 1 x 2) -> 2
```
Obviously it's not what you expected
 
 
With asyncLock, you can easily write your async critical section
```js
lock.acquire('key', function(cb) {
    // Concurrency safe
    redis.get('key', function(err, value) {
        redis.set('key', value * 2, cb);
    });
}, function(err, ret) {
});
```
 
## Get Started
 
```js
var AsyncLock = require('async-lock');
var lock = new AsyncLock();
 
/**
 * @param {String|Array} key     resource key or keys to lock
 * @param {function} fn     execute function
 * @param {function} cb     (optional) callback function, otherwise will return a promise
 * @param {Object} opts     (optional) options
 */
lock.acquire(key, function(done) {
    // async work
    done(err, ret);
}, function(err, ret) {
    // lock released
}, opts);
 
// Promise mode
lock.acquire(key, function() {
    // return value or promise
}, opts).then(function() {
    // lock released
});
```
 
## Error Handling
 
```js
// Callback mode
lock.acquire(key, function(done) {
    done(new Error('error'));
}, function(err, ret) {
    console.log(err.message) // output: error
});
 
// Promise mode
lock.acquire(key, function() {
    throw new Error('error');
}).catch(function(err) {
    console.log(err.message) // output: error
});
```
 
## Acquire multiple keys
 
```js
lock.acquire([key1, key2], fn, cb);
```
 
## Domain reentrant lock
 
Lock is reentrant in the same domain
 
```js
var domain = require('domain');
var lock = new AsyncLock({domainReentrant : true});
 
var d = domain.create();
d.run(function() {
    lock.acquire('key', function() {
        //Enter lock
        return lock.acquire('key', function() {
            //Enter same lock twice
        });
    });
});
```
 
## Options
 
```js
// Specify timeout
var lock = new AsyncLock({timeout: 5000});
lock.acquire(key, fn, function(err, ret) {
    // timed out error will be returned here if lock not acquired in given time
});
 
// Set max pending tasks
var lock = new AsyncLock({maxPending: 1000});
lock.acquire(key, fn, function(err, ret) {
    // Handle too much pending error
})
 
// Whether there is any running or pending async function
lock.isBusy();
 
// Use your own promise library instead of the global Promise variable
var lock = new AsyncLock({Promise: require('bluebird')}); // Bluebird
var lock = new AsyncLock({Promise: require('q')}); // Q
 
// Add a task to the front of the queue waiting for a given lock
lock.acquire(key, fn1, cb); // runs immediately
lock.acquire(key, fn2, cb); // added to queue
lock.acquire(key, priorityFn, cb, {skipQueue: true}); // jumps queue and runs before fn2
```
 
## Changelog
 
See [Changelog](./History.md)
 
## Issues
 
See [issue tracker](https://github.com/rogierschouten/async-lock/issues).
 
## License
 
MIT, see [LICENSE](./LICENSE)