# @hazae41/mutex

> Rust-like Mutex for TypeScript

Latest version **3.0.1** (published 2025-10-18) · 0 weekly downloads

## Install

```sh
npm install @hazae41/mutex
pnpm add @hazae41/mutex
yarn add @hazae41/mutex
bun add @hazae41/mutex
```

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2025-10-18 |
| First published | 2023-03-04 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 18.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | hazae41 |
| Maintainers | hazae41 |
| Keywords | mutex, promise, manual, lock, wait, semaphore, async, signal, typescript, esmodules, tested, unit-tested |

## Links

- npm: https://www.npmjs.com/package/@hazae41/mutex
- Repository: https://github.com/hazae41/mutex
- Homepage: https://github.com/hazae41/mutex#readme
- Issues: https://github.com/hazae41/mutex/issues
- npm.io page: https://npm.io/package/@hazae41/mutex

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@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

## Recent versions

- 3.0.1 (latest) — 2025-10-18
- 3.0.0 — 2025-10-04
- 2.2.3 — 2025-10-04
- 2.2.2 — 2025-06-18
- 2.2.1 — 2025-06-18
- 2.2.0 — 2025-06-15
- 2.1.0 — 2024-09-04
- 2.0.3 — 2024-09-04
- 2.0.2 — 2024-09-04
- 2.0.1 — 2024-08-31
- 2.0.0 — 2024-03-18
- 1.2.12 — 2023-10-30
- 1.2.11 — 2023-09-27
- 1.2.10 — 2023-08-29
- 1.2.9 — 2023-06-16
- … 10 more at https://npm.io/package/@hazae41/mutex/versions

## README

# Mutex

Rust-like Mutex and Semaphore for TypeScript

```bash
npm install @hazae41/mutex
```

```bash
deno install jsr:@hazae41/mutex
```

[**📦 NPM**](https://www.npmjs.com/package/@hazae41/mutex) • [**📦 JSR**](https://jsr.io/@hazae41/mutex)

## Features

### Current features
- 100% TypeScript and ESM
- No external dependencies
- Similar to Rust
- Can hold data
- Unit-tested
- Memory-safe

## Usage

### Mutex

```typescript
import { Mutex } from "@hazae41/mutex"

const mutex = new Mutex(123)

/**
 * You can queue a callback 
 */
async function runOrWait() {
  await mutex.runOrWait((x) => /* do (async) stuff with x */)
}

/**
 * You can throw if the mutex is already locked
 */
async function runOrThrow() {
  await mutex.runOrThrow((x) => /* do stuff with x */)
}

/**
 * You can return something from the callback
 */
async function runOrWait2() {
  const y = await mutex.runOrWait((x) => x * 2)
}

/**
 * You can use async code
 */
async function runOrWait3() {
  const y = await mutex.runOrWait(async (x) => await f(x))
}

/**
 * You can acquire and release when you want
 */
async function getOrWait() {
  const x = await mutex.getOrWait()
  const y = x.get() * 2
  x.release()
}

/**
 * You can acquire and release with `using`
 */
async function getOrWait2() {
  using x = await mutex.getOrWait()
  const y = x.get() * 2
}
```

### Semaphore

Same functions as Mutex but you can specify a capacity

```tsx
const semaphore = new Semaphore(123, 3)

const a = await semaphore.getOrThrow()
const b = await semaphore.getOrThrow()
const c = await semaphore.getOrThrow()
const d = await semaphore.getOrThrow() // will throw
```

You can see it like `Mutex<T> = Semaphore<T, 1>`

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