# @weegigs/concurrent

> Concurrency utilities for Typescript

Latest version **1.2.0** (published 2019-11-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install @weegigs/concurrent
pnpm add @weegigs/concurrent
yarn add @weegigs/concurrent
bun add @weegigs/concurrent
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2019-11-22 |
| First published | 2017-11-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 8.9.3 |
| Dependencies | 0 |
| Unpacked size | 13.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Kevin O'Neill |
| Maintainers | kevinoneill |
| Keywords | mutex, semaphore, typescript, async, await, concurrent, promise |

## Links

- npm: https://www.npmjs.com/package/@weegigs/concurrent
- Repository: https://github.com/kevinoneill/wee-concurrent
- Homepage: https://github.com/kevinoneill/wee-concurrent#readme
- Issues: https://github.com/kevinoneill/wee-concurrent/issues
- npm.io page: https://npm.io/package/@weegigs/concurrent

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 1.2.0 (latest) — 2019-11-22
- 1.1.0 — 2019-11-19
- 1.0.1 — 2018-06-14
- 1.0.0 — 2017-11-29

## README

# @weegigs/concurrent

Utilities for dealing with concurrency in Typescript (and Javascript).

[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)
[![Greenkeeper badge](https://badges.greenkeeper.io/kevinoneill/wee-concurrent.svg)](https://greenkeeper.io/)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![Maintainability](https://api.codeclimate.com/v1/badges/3ab8d078d6ff3f6d0ba9/maintainability)](https://codeclimate.com/github/kevinoneill/wee-concurrent/maintainability)

## Overview

Concurrent provides two handy classes when you want to limit the amount of concurrent work being executed in
`Promise`s, `Semaphore` and `Mutex`.

`Semaphore` and `Mutex` share a common interface `Gate`. The `Gate` interface provides two functions `acquire`
and `execute`.

### `acquire(timeout?: number): Promise<Release>`

If a timeout greater than zero is passed then a `TimeoutError` will be triggered if the duration in milliseconds
is exceeded.

```typescript
try {
  const release = await gate.acquire(10);
  // ... do some work ...
  release();
} catch (error) {
  release();
}
```

### `execute<T>(worker: Worker<T>, timeout?: number): Promise<T>;`

`execute` allows you to avoid managing the `Release` function by using a `Worker`. A `Worker` is a function from
`void` to `T` or `Promise<T>`.

As with `acquire`, if a timeout greater than zero is passed then a `TimeoutError` will be triggered if the
duration in milliseconds is exceeded.

```typescript
try {
  const result = await gate.execute(() => {
    // ... do some work ...
    return value;
  }, 10);

  // ...do something with the value...
} catch (error) {
  // ... do something with the error ...
}
```

## Todo

- [x] Semaphore
- [x] Mutex
- [x] Latch
- [ ] Example Usage
- [ ] Documentation

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