{"id":18563123,"url":"https://github.com/compulim/promise-critical-section","last_synced_at":"2025-04-10T03:32:38.062Z","repository":{"id":65475764,"uuid":"75074527","full_name":"compulim/promise-critical-section","owner":"compulim","description":"Critical section in Promise fashion","archived":false,"fork":false,"pushed_at":"2016-11-29T17:10:45.000Z","size":12,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-29T01:21:35.429Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/compulim.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-29T11:23:11.000Z","updated_at":"2022-05-31T20:46:42.000Z","dependencies_parsed_at":"2023-01-25T06:05:10.913Z","dependency_job_id":null,"html_url":"https://github.com/compulim/promise-critical-section","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compulim%2Fpromise-critical-section","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compulim%2Fpromise-critical-section/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compulim%2Fpromise-critical-section/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compulim%2Fpromise-critical-section/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/compulim","download_url":"https://codeload.github.com/compulim/promise-critical-section/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223424468,"owners_count":17142744,"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":[],"created_at":"2024-11-06T22:11:52.420Z","updated_at":"2024-11-06T22:11:53.142Z","avatar_url":"https://github.com/compulim.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# promise-critical-section\n\nAllows only one asynchronous operation to run at a time.\n\nAlthough JavaScript is single-threaded, there are times you may want to limit number of asynchronous operation to enter a block simultaneously. For example,\n\n* Pooling a single resource\n* Running a series of asynchronous steps without interruptions\n\n## How to use\n\nIn the code below, `Step 1A` and `Step 1B` will be run serially regardless of race condition from `Step 2`.\n\n```js\nimport CriticalSection from 'promise-critical-section';\n\nconst section = new CriticalSection();\n\nPromise.race([\n  section.enter()\n    .then(() =\u003e {\n      // do something that is limited to one asynchoronous operation\n      console.log('Step 1A');\n    })\n    .then(() =\u003e {\n      // do another thing\n      console.log('Step 1B');\n    })\n    .then(() =\u003e section.leave()),\n  section.enter()\n    .then(() =\u003e {\n      // do something in the critical section\n      console.log('Step 2');\n    })\n    .then(() =\u003e section.leave())\n])\n```\n\nYou can also write it with ES7 async/await. The following example maybe a bit exaggerated.\n\n```js\nreturn Promise.all([\n  (async () =\u003e {\n    await section.enter();\n    console.log('Step 1A');\n    console.log('Step 1B');\n    await section.leave();\n  })(),\n  (async () =\u003e {\n    await section.enter();\n    console.log('Step 2');\n    await section.leave();\n  })()\n]);\n```\n\n## Contributions\n\nLike us? Please [star](star) us or give us [suggestions](issues).\n\nPlease file an [issue](issues) to us with minimal repro.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcompulim%2Fpromise-critical-section","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcompulim%2Fpromise-critical-section","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcompulim%2Fpromise-critical-section/lists"}