{"_id":"p-lock","_rev":"203487","name":"p-lock","description":"Simple promise lock","dist-tags":{"latest":"3.0.0"},"maintainers":[{"name":"bconnorwhite","email":""},{"name":"npm:bconnorwhite","email":""}],"time":{"modified":"2023-03-21T13:23:45.000Z","created":"2021-04-06T01:28:19.501Z","3.0.0":"2023-01-10T22:24:01.117Z","2.1.0":"2021-04-24T20:17:52.027Z","2.0.0":"2021-04-06T02:17:20.065Z","1.0.0":"2021-04-06T01:28:19.501Z"},"users":{},"author":{"name":"Connor White"},"repository":{"type":"git","url":"git+https://github.com/bconnorwhite/p-lock.git"},"versions":{"3.0.0":{"name":"p-lock","version":"3.0.0","description":"Simple promise lock","main":".auto/build/index.js","author":{"name":"Connor White"},"license":"MIT","private":false,"type":"module","repository":{"type":"git","url":"git+https://github.com/bconnorwhite/p-lock.git"},"keywords":["promise","lock","simple","async"],"exports":"./.auto/build/index.js","types":".auto/build/index.d.ts","engines":{"node":"^14.13.1 || >=16.0.0"},"scripts":{"build":"auto build","dev":"auto dev","lint":"auto lint","prepack":"yarn build","release":"auto release","test":"auto test","typecheck":"auto typecheck"},"devDependencies":{"autorepo":"^0.1.9"},"eslintConfig":{"extends":"@autosoft/eslint-config"},"jest":{"preset":"@autosoft/jest-preset"},"npmpackagejsonlint":{"extends":"npm-package-json-lint-config-auto"},"_id":"p-lock@3.0.0","dist":{"shasum":"6a14783804cec3ef7d453385414bf9770264649f","size":2808,"noattachment":false,"key":"/p-lock/-/p-lock-3.0.0.tgz","tarball":"http://name.csiicloud.com:7001/p-lock/download/p-lock-3.0.0.tgz"},"_npmUser":{"name":"bconnorwhite","email":"connor.bcw@gmail.com"},"directories":{},"maintainers":[{"name":"bconnorwhite","email":""},{"name":"npm:bconnorwhite","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/p-lock_3.0.0_1673389440884_0.043793686746246285"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2023-01-10T22:24:01.117Z","publish_time":1673389441117,"_cnpm_publish_time":1673389441117},"2.1.0":{"name":"p-lock","version":"2.1.0","description":"Simple promise lock","license":"MIT","private":false,"author":{"name":"Connor White","email":"connor.bcw@gmail.com","url":"https://connorwhite.org"},"homepage":"https://connorwhite.org/github/p-lock","repository":{"type":"git","url":"git+https://github.com/bconnorwhite/p-lock.git"},"keywords":["promise","lock","simple","async"],"main":"./build/index.js","scripts":{"build":"bob build","commit":"bob commit","lint":"bob lint","release":"bob publish","test":"bob test"},"devDependencies":{"@bconnorwhite/bob":"^2.9.4"},"eslintConfig":{"extends":"eslint-config-bob"},"husky":{"hooks":{"commit-msg":"bob lint commit"}},"npmpackagejsonlint":{"extends":"npm-package-json-lint-config-bob"},"_id":"p-lock@2.1.0","dist":{"shasum":"6f9dbc55d3aac5b0f75c8ec47f3a6f1b954135f8","size":2538,"noattachment":false,"key":"/p-lock/-/p-lock-2.1.0.tgz","tarball":"http://name.csiicloud.com:7001/p-lock/download/p-lock-2.1.0.tgz"},"_npmUser":{"name":"bconnorwhite","email":"connor.bcw@gmail.com"},"directories":{},"maintainers":[{"name":"bconnorwhite","email":""},{"name":"npm:bconnorwhite","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/p-lock_2.1.0_1619295471801_0.026035470851726528"},"_hasShrinkwrap":false,"publish_time":1619295472027,"_cnpm_publish_time":1619295472027,"_cnpmcore_publish_time":"2021-12-16T20:55:48.085Z"},"2.0.0":{"name":"p-lock","version":"2.0.0","description":"Simple promise lock","license":"MIT","private":false,"author":{"name":"Connor White","email":"connor.bcw@gmail.com","url":"https://connorwhite.org"},"homepage":"https://connorwhite.org/github/p-lock","repository":{"type":"git","url":"git+https://github.com/bconnorwhite/p-lock.git"},"keywords":["promise","lock","simple","async"],"main":"./build/index.js","scripts":{"build":"bob build","commit":"bob commit","lint":"bob lint","release":"bob publish","test":"bob test"},"devDependencies":{"@bconnorwhite/bob":"^2.9.4"},"eslintConfig":{"extends":"eslint-config-bob"},"husky":{"hooks":{"commit-msg":"bob lint commit"}},"npmpackagejsonlint":{"extends":"npm-package-json-lint-config-bob"},"_id":"p-lock@2.0.0","dist":{"shasum":"04bf81ce040f9669cbb4eab297d52ee75a8aa32c","size":1806,"noattachment":false,"key":"/p-lock/-/p-lock-2.0.0.tgz","tarball":"http://name.csiicloud.com:7001/p-lock/download/p-lock-2.0.0.tgz"},"_npmUser":{"name":"bconnorwhite","email":"connor.bcw@gmail.com"},"directories":{},"maintainers":[{"name":"bconnorwhite","email":""},{"name":"npm:bconnorwhite","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/p-lock_2.0.0_1617675439934_0.09723231519011288"},"_hasShrinkwrap":false,"publish_time":1617675440065,"_cnpm_publish_time":1617675440065,"_cnpmcore_publish_time":"2021-12-16T20:55:48.302Z"},"1.0.0":{"name":"p-lock","version":"1.0.0","description":"Simple promise lock","license":"MIT","private":false,"author":{"name":"Connor White","email":"connor.bcw@gmail.com","url":"https://connorwhite.org"},"homepage":"https://connorwhite.org/github/p-lock","repository":{"type":"git","url":"git+https://github.com/bconnorwhite/p-lock.git"},"keywords":["promise","lock","simple","async"],"main":"./build/index.js","scripts":{"build":"bob build","commit":"bob commit","lint":"bob lint","release":"bob publish","test":"bob test"},"devDependencies":{"@bconnorwhite/bob":"^2.9.4"},"eslintConfig":{"extends":"eslint-config-bob"},"husky":{"hooks":{"commit-msg":"bob lint commit"}},"npmpackagejsonlint":{"extends":"npm-package-json-lint-config-bob"},"_id":"p-lock@1.0.0","dist":{"shasum":"8a8bf8b587d45da4ed6dc3c136724bae963aa024","size":1647,"noattachment":false,"key":"/p-lock/-/p-lock-1.0.0.tgz","tarball":"http://name.csiicloud.com:7001/p-lock/download/p-lock-1.0.0.tgz"},"_npmUser":{"name":"bconnorwhite","email":"connor.bcw@gmail.com"},"directories":{},"maintainers":[{"name":"bconnorwhite","email":""},{"name":"npm:bconnorwhite","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/p-lock_1.0.0_1617672499365_0.9231049250337451"},"_hasShrinkwrap":false,"publish_time":1617672499501,"_cnpm_publish_time":1617672499501,"_cnpmcore_publish_time":"2021-12-16T20:55:48.506Z"}},"readme":"<!--BEGIN HEADER-->\n<div id=\"top\" align=\"center\">\n  <h1>p-lock</h1>\n  <a href=\"https://npmjs.com/package/p-lock\">\n    <img alt=\"NPM\" src=\"https://img.shields.io/npm/v/p-lock.svg\">\n  </a>\n  <a href=\"https://github.com/bconnorwhite/p-lock\">\n    <img alt=\"TypeScript\" src=\"https://img.shields.io/github/languages/top/bconnorwhite/p-lock.svg\">\n  </a>\n  <a href=\"https://coveralls.io/github/bconnorwhite/p-lock?branch=master\">\n    <img alt=\"Coverage Status\" src=\"https://img.shields.io/coveralls/github/bconnorwhite/p-lock.svg?branch=master\">\n  </a>\n</div>\n\n<br />\n\n<blockquote align=\"center\">Simple promise lock.</blockquote>\n\n<br />\n\n_If I should maintain this repo, please ⭐️_\n<a href=\"https://github.com/bconnorwhite/p-lock\">\n  <img align=\"right\" alt=\"GitHub stars\" src=\"https://img.shields.io/github/stars/bconnorwhite/p-lock?label=%E2%AD%90%EF%B8%8F&style=social\">\n</a>\n\n_DM me on [Twitter](https://twitter.com/bconnorwhite) if you have questions or suggestions._\n<a href=\"https://twitter.com/bconnorwhite\">\n  <img align=\"right\" alt=\"Twitter\" src=\"https://img.shields.io/twitter/url?label=%40bconnorwhite&style=social&url=https%3A%2F%2Ftwitter.com%2Fbconnorwhite\">\n</a>\n\n---\n<!--END HEADER-->\n\n## About\n\nThis package provides a simple promise lock, which is useful for preventing race conditions between multiple promises.\n\n## Installation\n\n```sh\nyarn add p-lock\n```\n\n```sh\nnpm install p-lock\n```\n\n```sh\npnpm install p-lock\n```\n\n## Overview\n\nFirst, we get a lock function:\n\n```ts\nimport { getLock } from \"p-lock\";\n\nconst lock = getLock();\n```\n\nCalling the lock function returns a promise that resolves when the lock is acquired.\n\nThe promise resolves with a release function, which must be called to release the lock:\n\n```ts\nlock(\"example-key\").then((release) => {\n  // Now I have the lock for \"example-key\"\n  // do something...\n  release();\n});\n```\n\n\n## Full Example\n\nIn this example, we have two promises writing to the same file. However, we want to ensure that the first one finishes before the second one starts.\n\n```ts\nimport { writeFile } from \"fs\";\nimport { getLock } from \"p-lock\";\n\nconst lock = getLock();\n\nlock(\"file\").then((release) => {\n  setTimeout(() => {\n    writeFile(\"test.txt\", \"hello\", () => {\n      release();\n    });\n  }, 1000);\n});\n\nlock(\"file\").then((release) => {\n  writeFile(\"test.txt\", \"world\", () => {\n    release();\n  });\n});\n\n// Contents of test.txt will be \"world\"\n```\n\n### Replace\n\nIn some cases, we may want to replace an existing promise waiting for a lock, rather than waiting for it to finish.\n\nThe `replace` option allows us to do this:\n\n```ts\nimport { writeFile } from \"fs\";\nimport { getLock } from \"p-lock\";\n\nconst lock = getLock({ replace: true });\n\nlet writeCounter = 0;\n\nlock(\"file\").then((release) => {\n  setTimeout(() => {\n    writeCounter += 1;\n    writeFile(\"test.txt\", `update #${writeCounter}`, () => {\n      release();\n    });\n  }, 1000);\n});\n\nlock(\"file\").then((release) => {\n  writeCounter += 1;\n  writeFile(\"test.txt\", `update #${writeCounter}`, () => {\n    release();\n  });\n}).catch(() => {\n  // This promise will reject, since the next one replaces.\n});\n\nlock(\"file\").then((release) => {\n  writeCounter += 1;\n  writeFile(\"test.txt\", `update #${writeCounter}`, () => {\n    release();\n  });\n});\n\n// Contents of test.txt will be \"update #2\"\n```\n\n<!--BEGIN FOOTER-->\n\n<br />\n\n<h2>Dev Dependencies</h2>\n\n- [autorepo](https://www.npmjs.com/package/autorepo): Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.\n\n<br />\n\n<h2 id=\"license\">License <a href=\"https://opensource.org/licenses/MIT\"><img align=\"right\" alt=\"license\" src=\"https://img.shields.io/npm/l/p-lock.svg\"></a></h2>\n\n[MIT](https://opensource.org/licenses/MIT) - _MIT License_\n<!--END FOOTER-->\n","_attachments":{},"license":"MIT"}