npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

async-mutex-v2

v2.1.0

Published

A fast, lightweight, Promise-based mutex for JavaScript and TypeScript applications.

Downloads

448

Readme

async-mutex-v2

A fast, lightweight, Promise-based mutex for JavaScript and TypeScript applications.

npm version License: MIT Node.js

async-mutex-v2 is a modern synchronization library designed for asynchronous JavaScript environments. It provides a simple, reliable way to protect critical sections of code, preventing race conditions when multiple asynchronous operations compete for shared resources.

Whether you're building trading bots, API services, distributed workers, task schedulers, or high-concurrency applications, async-mutex-v2 helps ensure that sensitive operations execute safely and sequentially.


Why async-mutex-v2?

JavaScript is single-threaded, but asynchronous operations frequently execute concurrently. When multiple tasks modify the same resource simultaneously, unexpected behavior can occur.

async-mutex-v2 serializes access to critical sections, ensuring that only one asynchronous operation holds the lock at any given time.

Without a Mutex

Task A
Task B
Task C

Shared balance:
100
+20
-10
+50

Final balance may become incorrect.

With async-mutex-v2

Task A acquires lock
Task A completes
Task B acquires lock
Task B completes
Task C acquires lock
Task C completes

Shared resource remains consistent.

Features

  • Lightweight with minimal overhead
  • Promise-based API
  • Zero runtime dependencies
  • Automatic lock management
  • Manual lock acquisition support
  • TypeScript support
  • CommonJS and ES Module compatible
  • Predictable FIFO lock queue
  • Suitable for high-concurrency workloads
  • Easy integration into existing projects

Installation

npm install async-mutex-v2

or

yarn add async-mutex-v2

or

pnpm add async-mutex-v2

Quick Start

Using runExclusive()

const { Mutex } = require("async-mutex-v2");

const mutex = new Mutex();

await mutex.runExclusive(async () => {
    console.log("Protected code");
});

Manual Lock

const { Mutex } = require("async-mutex-v2");

const mutex = new Mutex();

const release = await mutex.acquire();

try {
    console.log("Critical section");
}
finally {
    release();
}

Example

Imagine several requests attempting to update the same database record.

const { Mutex } = require("async-mutex-v2");

const mutex = new Mutex();

let counter = 0;

async function increment() {
    await mutex.runExclusive(async () => {
        const current = counter;

        await new Promise(resolve => setTimeout(resolve, 100));

        counter = current + 1;
    });
}

await Promise.all([
    increment(),
    increment(),
    increment(),
    increment(),
    increment()
]);

console.log(counter);

Output

5

Without synchronization, the result could be unpredictable.


API Reference

new Mutex()

Creates a new mutex instance.

const mutex = new Mutex();

acquire()

Acquires the mutex.

Returns a Promise that resolves to a release function.

const release = await mutex.acquire();

try {

    // Critical section

}
finally {

    release();

}

runExclusive(callback)

Runs a callback while holding the mutex.

The lock is automatically released when the callback completes or throws an error.

await mutex.runExclusive(async () => {

    // Protected code

});

TypeScript

import { Mutex } from "async-mutex-v2";

const mutex = new Mutex();

await mutex.runExclusive(async () => {

    console.log("TypeScript supported");

});

Real-World Use Cases

async-mutex-v2 is commonly useful in applications such as:

  • Cryptocurrency trading bots
  • Prediction market bots
  • Automated arbitrage systems
  • REST API servers
  • Express.js middleware
  • Database transaction coordination
  • Redis cache synchronization
  • Background workers
  • Queue processors
  • Payment systems
  • File processing pipelines
  • Scheduled jobs
  • Inventory management
  • Financial applications
  • Distributed task execution

Best Practices

  • Keep critical sections as short as possible.
  • Always release manually acquired locks inside a finally block.
  • Avoid performing unnecessary I/O while holding a lock.
  • Prefer runExclusive() for cleaner, safer code.
  • Create separate mutexes for unrelated shared resources.

Performance

async-mutex-v2 is designed with performance in mind.

  • Lightweight implementation
  • Minimal memory footprint
  • Efficient Promise queue
  • FIFO lock scheduling
  • Suitable for long-running Node.js services

Compatibility

| Runtime | Supported | | ----------- | --------- | | Node.js 16+ | ✅ | | Node.js 18+ | ✅ | | Node.js 20+ | ✅ | | Node.js 22+ | ✅ | | CommonJS | ✅ | | ES Modules | ✅ | | TypeScript | ✅ |


Contributing

Contributions are welcome.

If you discover a bug, have an idea for a new feature, or want to improve the documentation, feel free to open an issue or submit a pull request.


License

MIT License

Copyright (c) 2026


Keywords

mutex
async
lock
synchronization
promise
queue
typescript
nodejs
javascript
concurrency
race-condition
critical-section