Gathering detailed insights and metrics for @algonomia/semaphore-async-await
Gathering detailed insights and metrics for @algonomia/semaphore-async-await
npm install @algonomia/semaphore-async-await
Typescript
Module System
Min. Node Version
Node Version
NPM Version
71.9
Supply Chain
99.3
Quality
74.8
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
4,003
Last Day
1
Last Week
6
Last Month
58
Last Year
1,183
74 Commits
1 Watching
1 Branches
1 Contributors
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
Cumulative downloads
Total Downloads
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
7
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.)
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()
.
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.
yarn add semaphore-async-await
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})();
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
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
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
69 existing vulnerabilities detected
Details
Score
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