# promise-critical-section

> Allows only one asynchronous operation to run every time.

Latest version **0.0.2** (published 2016-11-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install promise-critical-section
pnpm add promise-critical-section
yarn add promise-critical-section
bun add promise-critical-section
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2016-11-29 |
| First published | 2016-11-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 6.0.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | William Wong |
| Maintainers | compulim |

## Links

- npm: https://www.npmjs.com/package/promise-critical-section
- Repository: https://github.com/compulim/promise-critical-section
- Homepage: https://github.com/compulim/promise-critical-section#readme
- Issues: https://github.com/compulim/promise-critical-section/issues
- npm.io page: https://npm.io/package/promise-critical-section

## Recent versions

- 0.0.2 (latest) — 2016-11-29
- 0.0.1 — 2016-11-29

## README

# promise-critical-section

Allows only one asynchronous operation to run every 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 one-by-one

## 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.

---
_Source: https://npm.io/package/promise-critical-section · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
