https://github.com/compulim/promise-critical-section
Critical section in Promise fashion
https://github.com/compulim/promise-critical-section
Last synced: about 1 month ago
JSON representation
Critical section in Promise fashion
- Host: GitHub
- URL: https://github.com/compulim/promise-critical-section
- Owner: compulim
- License: mit
- Created: 2016-11-29T11:23:11.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-11-29T17:10:45.000Z (over 8 years ago)
- Last Synced: 2024-10-29T01:21:35.429Z (7 months ago)
- Language: JavaScript
- Size: 11.7 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# promise-critical-section
Allows only one asynchronous operation to run at a time.
Although JavaScript is single-threaded, there are times you may want to limit number of asynchronous operation to enter a block simultaneously. For example,
* Pooling a single resource
* Running a series of asynchronous steps without interruptions## How to use
In the code below, `Step 1A` and `Step 1B` will be run serially regardless of race condition from `Step 2`.
```js
import CriticalSection from 'promise-critical-section';const section = new CriticalSection();
Promise.race([
section.enter()
.then(() => {
// do something that is limited to one asynchoronous operation
console.log('Step 1A');
})
.then(() => {
// do another thing
console.log('Step 1B');
})
.then(() => section.leave()),
section.enter()
.then(() => {
// do something in the critical section
console.log('Step 2');
})
.then(() => section.leave())
])
```You can also write it with ES7 async/await. The following example maybe a bit exaggerated.
```js
return Promise.all([
(async () => {
await section.enter();
console.log('Step 1A');
console.log('Step 1B');
await section.leave();
})(),
(async () => {
await section.enter();
console.log('Step 2');
await section.leave();
})()
]);
```## Contributions
Like us? Please [star](star) us or give us [suggestions](issues).
Please file an [issue](issues) to us with minimal repro.