https://github.com/leecjson/node-locked-sync
Synchronize and serialize a piece of code
https://github.com/leecjson/node-locked-sync
javascript node nodejs sync synchronization
Last synced: about 2 months ago
JSON representation
Synchronize and serialize a piece of code
- Host: GitHub
- URL: https://github.com/leecjson/node-locked-sync
- Owner: leecjson
- License: mit
- Created: 2018-11-27T06:06:00.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-11-27T10:32:10.000Z (over 7 years ago)
- Last Synced: 2025-08-09T10:12:00.506Z (11 months ago)
- Topics: javascript, node, nodejs, sync, synchronization
- Language: JavaScript
- Homepage:
- Size: 6.84 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Usage
```shell
npm install --save locked-sync
```
Synchronize and serialize a piece of code.
Why do you need this in javascript or Node.js.
Consider the following code
```javascript
redis.get('key', function(err, value) {
redis.set('key', value + 1);
});
```
If two users run concurrency, the execution order may like this
```text
user1: var val = redis.get('key'); => 1
user2: var val = redis.get('key'); => 1
user1: redis.set('key', val + 1) => 2
user2: redis.set('key', val + 1) => 2
```
So, you can use locked-sync to avoid it.
```javascript
const lockedSync = require('locked-sync');
const sync = lockedSync();
function getAndSet() {
sync().then(end => {
redis.get('key', (val, err) => {
redis.set('key', val + 1);
end();
});
});
}
getAndSet(); getAndSet(); getAndSet(); getAndSet(); // almost same time to get and set
```
```javascript
const lockedSync = require('locked-sync');
const sync = lockedSync();
async function getAndSet() {
const end = await sync();
try {
const val = await redis.get('key');
await redis.set('key', val + 1);
} finally {
end(); // Always put it in finally block.
}
}
getAndSet(); getAndSet(); getAndSet(); getAndSet();
```