{"id":18373183,"url":"https://github.com/mgtitimoli/await-mutex","last_synced_at":"2025-04-06T19:32:16.219Z","repository":{"id":57142823,"uuid":"48557323","full_name":"mgtitimoli/await-mutex","owner":"mgtitimoli","description":"Promised based Javascript Mutex","archived":false,"fork":false,"pushed_at":"2018-08-29T14:17:41.000Z","size":11,"stargazers_count":43,"open_issues_count":3,"forks_count":7,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-22T05:51:08.859Z","etag":null,"topics":["async-await","async-programming","javascript","mutex","mutex-synchronisation","nodejs","promises"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mgtitimoli.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-24T21:33:35.000Z","updated_at":"2025-02-07T09:37:34.000Z","dependencies_parsed_at":"2022-09-05T22:30:13.967Z","dependency_job_id":null,"html_url":"https://github.com/mgtitimoli/await-mutex","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/mgtitimoli%2Fawait-mutex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgtitimoli%2Fawait-mutex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgtitimoli%2Fawait-mutex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgtitimoli%2Fawait-mutex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mgtitimoli","download_url":"https://codeload.github.com/mgtitimoli/await-mutex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247539276,"owners_count":20955285,"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-await","async-programming","javascript","mutex","mutex-synchronisation","nodejs","promises"],"created_at":"2024-11-06T00:09:15.335Z","updated_at":"2025-04-06T19:32:15.410Z","avatar_url":"https://github.com/mgtitimoli.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# await-mutex\n\nPromised based Mutex for cases where you need to synchronize sequentially the access to a single resource from multiple locations.\n\nA typical use case for a mutex is when multiple asynchronous processes are fired and all of them have to execute another, **but the same**, asynchronous process as they arrive, one at a time, waiting for previous call (if any) to finish before calling it again.\n\n## Examples\n\n### file-appender.js\n\nLet you create an object to perform file appends **one at a time**.\n\n```js\nimport * as fs from \"fs\";\nimport Mutex from \"await-mutex\";\n\nexport default class FileAppender {\n\n    constructor(filename) {\n\n        this._filename = filename;\n        this._mutex = new Mutex();\n    }\n\n    async append(data, options = undefined) {\n\n        let unlock = await this._mutex.lock();\n\n        fs.appendFile(this._filename, data, options, error =\u003e {\n\n            unlock();\n\n            if (error) {\n                throw error;\n            }\n        });\n    }\n}\n```\n\n## API\n\n```js\nimport Mutex from \"await-mutex\";\n```\n\n### Mutex\n\nCreates an instance of Mutex (can not be called without **new**).\n\n```js\nlet mutex = new Mutex();\n```\n\n### Mutex.prototype.isLocked\n\nReturns if the mutex instance is (true) or not locked (false).\n\n```js\nlet unlock = await mutex.lock();\n\nconsole.log(mutex.isLocked()); // prints true\n```\n\n### Mutex.prototype.lock: Promise\n\n- Waits until the mutex is unlocked and then locks it.\n- It returns an [ES2015 standard Promise](https://tc39.github.io/ecma262/#sec-promise-objects) (this allows the use of [async/await](http://tc39.github.io/ecmascript-asyncawait/)) which gets resolved once the mutex is unlocked.\n- The promise resolution value is an **unlock function** that has to be called once the mutex needs to be unlocked.\n\n```js\nasync function someFunc(mutex) {\n\n    let unlock = await mutex.lock(); // wait until mutex is unlocked\n\n    setTimeout(unlock, 3000);\n\n    console.log(someFunc.name);\n}\n\nasync function someOtherFunc(mutex) {\n\n    let unlock = await mutex.lock(); // wait until mutex is unlocked\n\n    console.log(someOtherFunc.name);\n}\n\nlet mutex = new Mutex();\n\nsomeFunc(mutex); // prints SomeFunc inmediately\nsomeOtherFunc(mutex); // waits 3 secs for mutex to be unlocked and then prints SomeOtherFunc\n```\n\n## Installation\n\nWith [npm](http://npmjs.org) do:\n\n```\nnpm install --save await-mutex\n```\n\n## Contributing\n\n### Contributors\n\n- [Mauro Titimoli](https://github.com/mgtitimoli)\n\n### How to\n\nTake a look to the [Contributing Guide](CONTRIBUTING.md)\n\n## license\n\n[Unlicense](http://unlicense.org/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgtitimoli%2Fawait-mutex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmgtitimoli%2Fawait-mutex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgtitimoli%2Fawait-mutex/lists"}