{"_id":"p-mutex","_rev":"2-ba3ce494652020f6b7b170eacb0bb417","name":"p-mutex","dist-tags":{"latest":"1.0.0"},"versions":{"0.1.0":{"name":"p-mutex","version":"0.1.0","keywords":["promise","lock","mutex","mutual","exclusion","async","await","semaphore","sema","locking","wait","race","promises","concurrently","concurrency","parallel","resource","guard"],"author":{"url":"https://sindresorhus.com","name":"Sindre Sorhus","email":"sindresorhus@gmail.com"},"license":"MIT","_id":"p-mutex@0.1.0","maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"homepage":"https://github.com/sindresorhus/p-mutex#readme","bugs":{"url":"https://github.com/sindresorhus/p-mutex/issues"},"dist":{"shasum":"fe6417947a4db6c49d7d281d88185a1f6235ccba","tarball":"https://registry.npmjs.org/p-mutex/-/p-mutex-0.1.0.tgz","fileCount":5,"integrity":"sha512-JXTGUNjI6eLzbtYGhFqmad8Tp5wDBsaGDwrvXuoRzIZ6EyKZr5jlNO9sFe83T6/K//lhyONS4ejCocvGNYJz7Q==","signatures":[{"sig":"MEUCIQCIjaOSCDvuCbH8NX3aJdtb6T4/NyLY/zU2QvXMHzexDQIgY0paF7PlNasn/6Xdf8mGiY4fGQqDaiSOfhitQdlijlE=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":6588},"type":"module","types":"./index.d.ts","engines":{"node":">=18"},"exports":{"types":"./index.d.ts","default":"./index.js"},"funding":"https://github.com/sponsors/sindresorhus","gitHead":"8e7992efc0fb4df45be4d875a9fed50c48dc4e87","scripts":{"test":"xo && ava && tsc index.d.ts"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"repository":{"url":"git+https://github.com/sindresorhus/p-mutex.git","type":"git"},"_npmVersion":"10.5.2","description":"Async mutex lock for managing access to a shared resource","directories":{},"sideEffects":false,"_nodeVersion":"18.20.2","dependencies":{"yocto-queue":"^1.0.0"},"_hasShrinkwrap":false,"devDependencies":{"xo":"^0.58.0","ava":"^6.1.2","typescript":"^5.4.5"},"_npmOperationalInternal":{"tmp":"tmp/p-mutex_0.1.0_1714037584678_0.9916855545408279","host":"s3://npm-registry-packages"}},"1.0.0":{"name":"p-mutex","version":"1.0.0","description":"Async mutex lock for managing access to a shared resource","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/p-mutex.git"},"funding":"https://github.com/sponsors/sindresorhus","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","exports":{"types":"./index.d.ts","default":"./index.js"},"sideEffects":false,"engines":{"node":">=20"},"scripts":{"test":"xo && ava && tsc index.d.ts"},"keywords":["promise","lock","mutex","mutual","exclusion","async","await","semaphore","sema","locking","wait","race","promises","concurrently","concurrency","parallel","resource","guard"],"dependencies":{"yocto-queue":"^1.2.1"},"devDependencies":{"ava":"^6.4.0","typescript":"^5.8.3","xo":"^0.60.0"},"_id":"p-mutex@1.0.0","gitHead":"c41a57e6a0f9feddaac1aee09b38dba9bf6ca3f4","types":"./index.d.ts","bugs":{"url":"https://github.com/sindresorhus/p-mutex/issues"},"homepage":"https://github.com/sindresorhus/p-mutex#readme","_nodeVersion":"20.19.1","_npmVersion":"10.9.2","dist":{"integrity":"sha512-UlthGzEMsg2VnZAR58wkzL7muskxtNamoTR1Q6/VYBUKqPaMM+YtSncjWIvyjfUvVECKck1SYC/4XIWWJU3gBw==","shasum":"7c7bdb30bef4a040ce3c87da76c7d7ac0d7b5f60","tarball":"https://registry.npmjs.org/p-mutex/-/p-mutex-1.0.0.tgz","fileCount":5,"unpackedSize":6588,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQDu74b1jgxFF8Ijmas14ZKrpxjnUE8VIIP40gwPYQblrQIhANPgfNT2L0FqMvjOKXUvbx6uscH25QqKnt+HjofllW4A"}]},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com","actor":{"name":"sindresorhus","email":"sindresorhus@gmail.com","type":"user"}},"directories":{},"maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/p-mutex_1.0.0_1751805303763_0.1463645899622501"},"_hasShrinkwrap":false}},"time":{"created":"2024-04-25T09:33:04.677Z","modified":"2025-07-06T12:35:04.090Z","0.1.0":"2024-04-25T09:33:04.845Z","1.0.0":"2025-07-06T12:35:03.939Z"},"bugs":{"url":"https://github.com/sindresorhus/p-mutex/issues"},"author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"license":"MIT","homepage":"https://github.com/sindresorhus/p-mutex#readme","keywords":["promise","lock","mutex","mutual","exclusion","async","await","semaphore","sema","locking","wait","race","promises","concurrently","concurrency","parallel","resource","guard"],"repository":{"type":"git","url":"git+https://github.com/sindresorhus/p-mutex.git"},"description":"Async mutex lock for managing access to a shared resource","maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"readme":"# p-mutex\n\n> Async [mutex lock](https://en.wikipedia.org/wiki/Lock_(computer_science)) for managing access to a shared resource\n\nIt provides a safe and easy way to ensure that only one operation accesses a particular resource at a time, preventing race conditions and ensuring data integrity.\n\n## Install\n\n```sh\nnpm install p-mutex\n```\n\n## Usage\n\n```js\nimport Mutex from 'p-mutex';\n\nconst mutex = new Mutex();\n\nconst sharedArray = [];\n\nasync function addToSharedArray(item) {\n\tawait mutex.withLock(async () => {\n\t\tconst item = await getItem();\n\t\tsharedArray.push(item);\n\t});\n}\n\naddToSharedArray('A');\naddToSharedArray('B');\n```\n\n## API\n\n### `Mutex()`\n\nCreates a new mutex object.\n\n### `.withLock(task)`\n\nAutomatically manages the lock during the execution of the given `task`.\n\nIt ensures that the mutex is locked before the `task` executes and automatically releases the lock afterward, even if an error occurs during the execution.\n\nParameters:\n- `task`: A function that performs the actions you want to execute while the lock is held. It can be async.\n\nReturns the result of the `task` function.\n\n> [!TIP]\n> Prefer using this method for most use cases as it handles the complexities of lock management and is less prone to errors. Use the lock and unlock methods directly only when you need more control over the lock management process.\n\n### `.lock()`\n\nAcquires the lock.\n\nReturns a promise that resolves when the lock has been acquired.\n\n> [!IMPORTANT]\n> Ensure every `.lock()` is paired with `.unlock()`. Use a `try...finally` block for safe lock management; the finally block ensures the lock is released, even if an error occurs.\n\n### `.unlock()`\n\nReleases the lock.\n\n### `.isLocked`\n\nIndicates whether the mutex is currently locked.\n\n## Use cases\n\n- **Concurrent Database Updates:** Ensures that database write operations do not interfere with each other, maintaining data integrity.\n- **File Writing Operations:** Manages simultaneous file write access in apps, preventing file corruption.\n- **Initialization Synchronization:** Ensures that shared resources are initialized only once in an app, even when multiple async tasks attempt access concurrently.\n- **API Request Sequencing:** Coordinates the order of API calls that modify data on a server to prevent race conditions and maintain consistent state.\n","readmeFilename":"readme.md"}