Installations
npm install @algonomia/semaphore-async-await
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=4.1
Node Version
9.4.0
NPM Version
6.4.1
Score
71.9
Supply Chain
99.3
Quality
74.8
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Developer
Download Statistics
Total Downloads
4,003
Last Day
1
Last Week
6
Last Month
58
Last Year
1,183
GitHub Statistics
74 Commits
1 Watching
1 Branches
1 Contributors
Package Meta Information
Latest Version
1.0.0
Package Id
@algonomia/semaphore-async-await@1.0.0
Unpacked Size
41.92 kB
Size
10.56 kB
File Count
15
NPM Version
6.4.1
Node Version
9.4.0
Total Downloads
Cumulative downloads
Total Downloads
4,003
Last day
0%
1
Compared to previous day
Last week
-62.5%
6
Compared to previous week
Last month
-38.3%
58
Compared to previous month
Last year
-15.4%
1,183
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
7
JavaScript Semaphore
A promise-based semaphore implementation suitable to be used with async/await. (This fork includes a function to wait for all queued promises to finish.)
Spare me the details, all I need is a lock.
Just import { Lock } from 'semaphore-async-await'
, acquire the lock by calling await lock.acquire()
and release it when you're done by calling lock.release()
.
But JavaScript is single-threaded and doesn't need semaphores!
This package can be used to synchronize functions that span multiple iterations of the event loop and prevent other code from being executed while your function is waiting.
Suppose you have a function
1async function criticalFunction() { 2 const data = await getDataFromDb(); 3 const modifiedData = await asynchronouslyDoStuffWithData(data); 4 return writeDataBackToDb(modifiedData); 5}
Calling this function repeatedly could lead to overlapping read/writes. To avoid this problem, a lock can be added like so:
1const lock = new Semaphore(1); 2 3async function criticalFunctionSynchronous() { 4 await lock.acquire(); 5 6 try { 7 await criticalFunction(); 8 } finally { 9 lock.release(); 10 } 11}
Asynchronous functions like criticalFunction
are executed in multiple chunks of code on the event loop, this package makes it possible to enforce an ordering in these chunks.
Install
yarn add semaphore-async-await
API
Usage
1import Semaphore from 'semaphore-async-await'; 2 3(async () => { 4 5 // A Semaphore with one permit is a lock 6 const lock = new Semaphore(1); 7 8 // Helper function used to wait for the given number of milliseconds 9 const wait = (ms) => new Promise(r => setTimeout(r, ms)); 10 11 let globalVar = 0; 12 13 (async () => { 14 // This waits (without blocking the event loop) until a permit becomes available 15 await lock.wait(); 16 const localCopy = globalVar; 17 await wait(500); 18 globalVar = localCopy + 1; 19 // Signal releases the lock and lets other things run 20 lock.signal(); 21 })(); 22 23 // This returns false because the function above has acquired the lock 24 // and is scheduled to continue executing once the main function yields or 25 // returns 26 console.log(lock.tryAcquire() === false); 27 28 // Similar to the function above but using waitFor instead of wait. We 29 // give it five seconds to wait which is enough time for it to acquire 30 // the lock 31 (async () => { 32 // This waits for at least five seconds, trying to acquire a permit. 33 const didAcquireLock = await lock.waitFor(5000); 34 if (didAcquireLock) { 35 const localCopy = globalVar; 36 await wait(500); 37 globalVar = localCopy + 1; 38 // Signal releases the lock and lets other things run 39 lock.signal(); 40 } 41 })(); 42 43 // Alternative to using wait()/signal() directly 44 lock.execute(async () => { 45 const localCopy = globalVar; 46 await wait(500); 47 globalVar = localCopy + 1; 48 }); 49 50 // Wait for everything to finish 51 await lock.finishAll(); 52 53 console.log(globalVar === 3); 54})();
License
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
69 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-832h-xg76-4gv6
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-9vvw-cc9w-f27h
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-hr2v-3952-633q
- Warn: Project is vulnerable to: GHSA-h6ch-v84p-w6p9
- Warn: Project is vulnerable to: GHSA-ff7x-qrg7-qggm
- Warn: Project is vulnerable to: GHSA-qrmc-fj45-qfc2
- Warn: Project is vulnerable to: GHSA-pfrx-2q88-qq97
- Warn: Project is vulnerable to: GHSA-q42p-pg8m-cqh6
- Warn: Project is vulnerable to: GHSA-w457-6q6x-cgp9
- Warn: Project is vulnerable to: GHSA-62gr-4qp9-h98f
- Warn: Project is vulnerable to: GHSA-f52g-6jhx-586p
- Warn: Project is vulnerable to: GHSA-2cf5-4w76-r9qv
- Warn: Project is vulnerable to: GHSA-3cqr-58rm-57f8
- Warn: Project is vulnerable to: GHSA-g9r4-xpmj-mj65
- Warn: Project is vulnerable to: GHSA-q2c6-c6pm-g3gh
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-44pw-h2cw-w3vq
- Warn: Project is vulnerable to: GHSA-vfrc-7r7c-w9mx
- Warn: Project is vulnerable to: GHSA-7wwv-vh3v-89cq
- Warn: Project is vulnerable to: GHSA-jp4x-w63m-7wgm
- Warn: Project is vulnerable to: GHSA-c429-5p7v-vgjp
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-2pr6-76vf-7546
- Warn: Project is vulnerable to: GHSA-8j8c-7jfh-h6hx
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-fvqr-27wr-82fm
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-7px7-7xjx-hxm8
- Warn: Project is vulnerable to: GHSA-x5pg-88wf-qq4p
- Warn: Project is vulnerable to: GHSA-5v2h-r2cx-5xgj
- Warn: Project is vulnerable to: GHSA-rrrm-qjm4-v8hf
- Warn: Project is vulnerable to: GHSA-f9cm-qmx5-m98h
- Warn: Project is vulnerable to: GHSA-7wpw-2hjm-89gp
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-w9mr-4mfr-499f
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-6394-6h9h-cfjg
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-6g33-f262-xjp4
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4rq4-32rv-6wp6
- Warn: Project is vulnerable to: GHSA-64g7-mvw6-v9qj
- Warn: Project is vulnerable to: GHSA-2m39-62fm-q8r3
- Warn: Project is vulnerable to: GHSA-mf6x-7mm4-x2g7
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-g7q5-pjjr-gqvp
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
1.7
/10
Last Scanned on 2024-12-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More