# @broxus/await-semaphore

> Awaitable semaphore/mutex [Fork of await-semaphore]

Latest version **0.1.5** (published 2022-03-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install @broxus/await-semaphore
pnpm add @broxus/await-semaphore
yarn add @broxus/await-semaphore
bun add @broxus/await-semaphore
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.5 |
| Published | 2022-03-15 |
| First published | 2021-05-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Emma Kuo |
| Maintainers | geronimo, rexagon, pavlovdog_ |
| Keywords | promise, semaphore, mutex, typescript |

## Links

- npm: https://www.npmjs.com/package/@broxus/await-semaphore
- Repository: https://github.com/notenoughneon/await-semaphore
- Homepage: https://github.com/notenoughneon/await-semaphore#readme
- Issues: https://github.com/notenoughneon/await-semaphore/issues
- npm.io page: https://npm.io/package/@broxus/await-semaphore

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.1.5 (latest) — 2022-03-15
- 0.1.4 — 2021-07-27
- 0.1.3 — 2021-05-02

## README

# await-semaphore
> NOTE: it is a fork of old [`await-semaphore`](https://www.npmjs.com/package/await-semaphore) package but with some fixes

Awaitable semaphore/mutex

A semaphore implementation using ES6 promises and supporting 3 styles:

* async/await style (needs typescript)
* thunk style (automatic acquire/release)
* promise style

Also includes `Mutex` as a convenience for `new Semaphore(1)`.

## API

### new Semaphore(count: number)

Create a new semaphore with the given count.

```javascript
import {Semaphore} from 'await-semaphore';

var semaphore = new Semaphore(10);
```

### semaphore.acquire(): Promise<() => void>

Acquire the semaphore and returns a promise for the release function. Be sure to handle release for exception case.

```javascript
semaphore.acquire()
.then(release => {
    //critical section...
    doSomething()
    .then(res => {
        //...
        release();
    })
    .catch(err => {
        //...
        release();
    });
});
```

### semaphore.use<T>(thunk: () => Promise<T>): Promise<T>

Alternate method for using the semaphore by providing a thunk. This automatically handles acquire/release.

```javascript
semaphore.use(() => {
    //critical section...
});
```

### new Mutex()

An alias for `new Semaphore(1)`. Mutex has the same methods as Semaphore.

```javascript
import {Mutex} from 'await-semaphore';

var mutex = new Mutex();
```

## Examples

Create a version of `fetch()` with concurrency limited to 10.

### async/await style (typescript)

```typescript
var semaphore = new Semaphore(10);

async function niceFetch(url) {
    var release = await semaphore.acquire();
    var result = await fetch(url);
    release();
    return result;
}
```

### thunk style (javascript)

```javascript
var semaphore = new Semaphore(10);

function niceFetch(url) {
    return semaphore.use(() => fetch(url));
}
```

### promise style (javascript)

```javascript
var semaphore = new Semaphore(10);

function niceFetch(url) {
    return semaphore.acquire()
    .then(release => {
        return fetch(url)
        .then(result => {
            release();
            return result;
        });
    });
}
```

---
_Source: https://npm.io/package/@broxus/await-semaphore · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
