{"id":21500497,"url":"https://github.com/node-libraries/semaphore","last_synced_at":"2025-04-23T17:24:40.059Z","repository":{"id":183007439,"uuid":"657450718","full_name":"node-libraries/semaphore","owner":"node-libraries","description":null,"archived":false,"fork":false,"pushed_at":"2023-12-06T11:51:05.000Z","size":12,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T02:08:06.550Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/node-libraries.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-06-23T05:03:52.000Z","updated_at":"2024-12-03T05:51:04.000Z","dependencies_parsed_at":"2023-07-22T12:26:42.575Z","dependency_job_id":null,"html_url":"https://github.com/node-libraries/semaphore","commit_stats":null,"previous_names":["node-libraries/semaphore"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-libraries%2Fsemaphore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-libraries%2Fsemaphore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-libraries%2Fsemaphore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-libraries%2Fsemaphore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/node-libraries","download_url":"https://codeload.github.com/node-libraries/semaphore/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250478824,"owners_count":21437245,"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-23T17:41:56.986Z","updated_at":"2025-04-23T17:24:40.038Z","avatar_url":"https://github.com/node-libraries.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @node-libraries/semaphore\n\nImplement virtual semaphore for asynchronous processing\n\n## usage\n\n```ts\n// Importing packages\nimport { semaphore } from \"@node-libraries/semaphore\";\n// Instance Creation\nconst s = semaphore();\n// Synchronization Lock\nawait s.acquire();\n// Synchronization lock release\ns.release();\n// Wait until all locks are unlocked.\nawait s.all();\n```\n\n```ts\n// Specify maximum number of parallels\nconst s = semaphore(5);\n```\n\n## example\n\n```ts\nimport { semaphore } from \"@node-libraries/semaphore\";\n\nconst f = (value: string) =\u003e\n  new Promise\u003cvoid\u003e((resolve) =\u003e {\n    console.timeLog(\"debug\", value);\n    setTimeout(resolve, 1000);\n  });\n\nconst main = async () =\u003e {\n  console.time(\"debug\");\n  const s = semaphore();\n  [\"A\", \"B\", \"C\", \"D\", \"E\"].forEach(async (v) =\u003e {\n    await s.acquire();\n    await f(v);\n    s.release();\n  });\n  await s.all(); // Wait for everything to be finished.\n  console.timeLog(\"debug\", \"end\");\n};\nmain();\n\n/* Result\ndebug: 0.197ms A\ndebug: 1.014s B\ndebug: 2.027s C\ndebug: 3.039s D\ndebug: 4.040s E\ndebug: 5.050s end\n*/\n```\n\n```ts\nimport { semaphore } from \"@node-libraries/semaphore\";\n\nconst f = (value: string) =\u003e\n  new Promise\u003cvoid\u003e((resolve) =\u003e {\n    console.timeLog(\"debug\", value);\n    setTimeout(resolve, 1000);\n  });\n\nconst main = async () =\u003e {\n  console.time(\"debug\");\n  const s = semaphore(2);\n  [\"A\", \"B\", \"C\", \"D\", \"E\"].forEach(async (v) =\u003e {\n    await s.acquire();\n    await f(v);\n    s.release();\n  });\n  await s.all(); // Wait for everything to be finished.\n  console.timeLog(\"debug\", \"end\");\n};\nmain();\n\n/* Result\ndebug: 0.19ms A\ndebug: 1.826ms B\ndebug: 1.005s C\ndebug: 1.005s D\ndebug: 2.012s E\ndebug: 3.028s end\n*/\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-libraries%2Fsemaphore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnode-libraries%2Fsemaphore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-libraries%2Fsemaphore/lists"}