{"id":26786964,"url":"https://github.com/mvisat/async-rwlock","last_synced_at":"2025-04-19T19:34:31.113Z","repository":{"id":57185765,"uuid":"144676181","full_name":"mvisat/async-rwlock","owner":"mvisat","description":"Promise-based asynchronous readers-writers lock","archived":false,"fork":false,"pushed_at":"2019-02-04T16:08:55.000Z","size":63,"stargazers_count":6,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T12:17:25.139Z","etag":null,"topics":["async","concurrency","promise","rwlock","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mvisat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-14T06:10:27.000Z","updated_at":"2020-12-27T23:11:03.000Z","dependencies_parsed_at":"2022-09-06T04:10:22.468Z","dependency_job_id":null,"html_url":"https://github.com/mvisat/async-rwlock","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mvisat%2Fasync-rwlock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mvisat%2Fasync-rwlock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mvisat%2Fasync-rwlock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mvisat%2Fasync-rwlock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mvisat","download_url":"https://codeload.github.com/mvisat/async-rwlock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249780499,"owners_count":21324558,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","concurrency","promise","rwlock","typescript"],"created_at":"2025-03-29T12:17:29.021Z","updated_at":"2025-04-19T19:34:31.095Z","avatar_url":"https://github.com/mvisat.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# async-rwlock\nPromise-based asynchronous readers-writers lock. Timeout is also supported.\n\n## Install\n```\n$ npm install --save async-rwlock\n```\nor\n```\n$ yarn add async-rwlock\n```\n\n## Quick Usage\n\n```js\nconst RWLock = require('async-rwlock').RWLock;\n\nconst lock = new RWLock();\n\nlock.readLock()\n.then(() =\u003e {\n    console.log('read lock acquired');\n    lock.unlock();\n});\n\nlock.writeLock()\n.then(() =\u003e {\n    console.log('write lock acquired');\n    lock.unlock();\n});\n```\n\n### Async/Await\n\n```ts\nimport RWLock from 'async-rwlock';\n\nasync function testLock() {\n    const lock = new RWLock();\n\n    await lock.readLock();\n    console.log('read lock acquired');\n    lock.unlock();\n\n    await lock.writeLock();\n    console.log('write lock acquired');\n    lock.unlock();\n}\n\n(async () =\u003e { await testLock(); })();\n```\n\n## Advanced Usage\n\n### Timeout\n```js\nconst RWLock = require('async-rwlock').RWLock;\n\nconst lock = new RWLock();\n\nlock.readLock()\n.then(() =\u003e {\n    console.log('read lock acquired');\n    // lock.unlock() // oops!\n});\n\nlock.writeLock(1000) // 1 second\n.then(() =\u003e {\n    console.log('write lock will never be acquired');\n})\n.catch((err) =\u003e {\n    console.error('promise will be rejected if timeout occured');\n    console.error(err);\n});\n```\n\n#### Async/Await\n\n```ts\nimport RWLock from 'async-rwlock';\n\nasync function testLock() {\n    const lock = new RWLock();\n\n    await lock.readLock();\n    console.log('read lock acquired');\n    // lock.unlock() // oops!\n\n    try {\n        await lock.writeLock(1000); // 1 second\n        console.log('write lock will never be acquired');\n    } catch (err) {\n        console.error('error will be thrown if timeout occured');\n        console.error(err);\n    }\n}\n\n(async () =\u003e { await testLock(); })();\n```\n\n## API\n\n### `new RWLock()`\n- Returns: instance of `RWLock`.\n\n#### `readLock([timeout])`\n- `timeout?: number`. **Default**: `Infinity`.\n- Returns: `Promise\u003cvoid\u003e`.\n\n#### `writeLock([timeout])`\n- `timeout?: number`. **Default**: `Infinity`.\n- Returns: `Promise\u003cvoid\u003e`.\n\nAcquire a read/write lock.\n\n`timeout` is how long it will wait to acquire the lock before promise is rejected in milliseconds. If `timeout` is not in range `0 \u003c= timeout \u003c Infinity`, it will wait indefinitely.\n\n#### `unlock()`\n- Returns: `void`.\n\nRelease current lock. Must be called after operation using read/write lock is finished.\n\n#### `getState()`\n- Returns: `State`.\n\nGet current state of lock. The states are either `Idle`, `Reading`, or `Writing`.\n\n## License\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmvisat%2Fasync-rwlock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmvisat%2Fasync-rwlock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmvisat%2Fasync-rwlock/lists"}