async-mutex

Mutex and semaphore primitives for synchronizing async workflows in JavaScript and TypeScript.

Library
npm
v0.5.0
1,434 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
37 /100 Needs Attention
Development Activity 0
Maintenance 0
Community 48
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
86 /100 Excellent
Architecture 86
Code Quality 88
Innovation 88
Learning Curve 85

async-mutex is a small, dependency-light library that brings classic concurrency primitives — mutexes and semaphores — to JavaScript’s single-threaded, asynchronous execution model. Although JavaScript runs on one thread, interleaved async operations can still produce race conditions when several tasks touch shared state across multiple spins of the event loop. async-mutex solves this by letting you serialize or rate-limit access to a critical section with promise-based locks.

Written in TypeScript with full type definitions, it exposes both a promise style and an async/await style API, plus higher-order decorators like withTimeout and tryAcquire. It works in any environment supporting ES5, ES6 promises, and Array.isArray, ships CommonJS and ES module builds, and has a single tiny runtime dependency (tslib).

What You Get

  • A Mutex class for exclusive access, with runExclusive, manual acquire/release, isLocked, waitForUnlock, and cancel.
  • A Semaphore class supporting weighted acquisition and task priority for controlling parallel access to N resources.
  • withTimeout and tryAcquire decorators that reject when a lock cannot be obtained within a deadline or immediately.
  • First-class TypeScript typings plus CommonJS and ES module builds usable across Node and the browser.

Common Use Cases

  • Serializing multi-step message exchanges with a web worker or socket so overlapping calls do not corrupt shared state.
  • Limiting the number of concurrent HTTP requests or file operations with a weighted semaphore.
  • Guarding writes to an in-memory cache or database connection that is not safe under interleaved async access.

Under The Hood

Architecture The library is layered around a single core primitive. Semaphore.ts implements the real scheduling logic: a priority-ordered queue of pending acquirers, a separate list of weighted waitForUnlock waiters, and a dispatch loop (_dispatchQueue/_dispatchItem) that hands out idempotent releasers as the counter allows. Mutex.ts is a thin adapter that simply constructs a Semaphore(1) and delegates every method to it, so a mutex is literally a binary semaphore. withTimeout.ts and tryAcquire.ts are decorators that wrap an existing Mutex or Semaphore and race its acquisition against a setTimeout, with tryAcquire being withTimeout(sync, 0). index.ts re-exports the public surface (Mutex, Semaphore, their interfaces, withTimeout, tryAcquire, and the E_CANCELED/E_TIMEOUT/E_ALREADY_LOCKED error sentinels).

Tech Stack Pure TypeScript (5.4) targeting ES5+, with tslib as the only runtime dependency. The build runs tsc three times to emit CommonJS (lib), ES6 (es6), and MJS outputs, then bundles the MJS entrypoint with Rollup. Tests use Mocha with ts-node, nyc for coverage, and @sinonjs/fake-timers for deterministic timing; linting is ESLint plus Prettier.

Code Quality Code quality is high. The src tree is small and cohesive (eight files), each class implements an explicit interface (MutexInterface, SemaphoreInterface), and releasers are made idempotent via a called guard to prevent double-release bugs. A dedicated test directory covers the mutex, semaphore, timeout, and tryAcquire behaviors, including a shared semaphoreSuite and fake-timer-driven timing tests. Weight and priority inputs are validated with explicit thrown errors.

API Design The public API is deliberately ergonomic. runExclusive covers the common case in one call by acquiring, running a callback, and releasing in a finally; manual acquire/release is available for advanced control, and both promise-chaining and async/await styles are documented. Cancellation, timeouts, and non-blocking try-acquire are layered as composable decorators rather than constructor flags, and a thorough README plus a separate API.md keep the learning curve low.

Used by 27 apps in this directory

TypeScript
100%
Other

Activepieces

AI Assistants · Automation · Mcp

24,756

Open-source AI automation platform that converts 280+ workflow integrations into MCP servers for LLMs, with no-code builders and TypeScript extensibility.

View details
92
Repo Health
85
Technical
64
Dependency
Built with
TypeScript 100%
Updated 1 weeks ago
TypeScript
89%
MIT

Amical

AI Assistants · Note Taking · Voice AI

1,540

Local-first AI dictation that understands your active app — private, offline, and built for speed.

View details
82
Repo Health
82
Technical
68
Dependency
Built with
TypeScript 89%
Updated 2 weeks ago
TypeScript
68%
Apache 2.0

Appsmith

Automation · Developer Tools · No Code Platforms

40,959

Open-source low-code platform to build admin panels, dashboards, and internal tools connected to any database or API.

View details
93
Repo Health
79
Technical
66
Dependency
Built with
TypeScript 68%
Java 21%
Updated 1 weeks ago
Python
68%
Other

Baserow

Databases · No Code Platforms

6,012

Open-source no-code platform to build databases, apps, automations, and AI agents — self-hosted or cloud, with full data ownership.

View details
89
Repo Health
84
Technical
68
Dependency
Built with
Python 68%
JavaScript 16%
Vue 11%
Updated 1 weeks ago
TypeScript
97%
AGPL 3.0

Bigcapital

Invoicing Finance

3,916

Self-hostable double-entry accounting platform with invoicing, inventory, multi-currency, and real-time financial reporting for small and medium businesses.

View details
90
Repo Health
77
Technical
61
Dependency
Built with
TypeScript 97%
Updated 1 weeks ago
TypeScript
45%
AGPL 3.0

BrowserOS

AI Assistants · Browser

13,763

The open-source agentic Chromium browser with native AI agents, MCP server, and visual workflow automation — your data never leaves your machine.

View details
87
Repo Health
82
Technical
68
Dependency
Built with
TypeScript 45%
Rust 23%
Python 18%
Updated 1 weeks ago
TypeScript
99%
AGPL 3.0

Cherry Studio

AI Assistants

52,202

All-in-one AI desktop client with 300+ assistants and multi-model support

View details
89
Repo Health
84
Technical
70
Dependency
Built with
TypeScript 99%
Updated 1 weeks ago
TypeScript
84%
Apache 2.0

Continue

AI Code Assistants · AI Development · Automation

36,049

Open-source coding agent for VS Code, JetBrains, and CLI with support for 30+ LLM providers.

View details
73
Repo Health
88
Technical
62
Dependency
Built with
TypeScript 84%
Updated 1 weeks ago
TypeScript
97%
MIT

DenchClaw

AI Agents · CRM

1,656

An AI-native CRM you self-host on your own machine, built on the OpenClaw agent framework, with a DuckDB backend and pre-built agent skills for managing contacts, deals, reports, and 500+ outreach integrations from natural-language chat.

View details
47
Repo Health
73
Technical
71
Dependency
Built with
TypeScript 97%
Updated 4 months ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers