Gathering detailed insights and metrics for semaphore-async-await
Gathering detailed insights and metrics for semaphore-async-await
Gathering detailed insights and metrics for semaphore-async-await
Gathering detailed insights and metrics for semaphore-async-await
🎌 Promise based Semaphore suitable to be used with async/await.
npm install semaphore-async-await
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.3
Supply Chain
100
Quality
75.7
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
20,054,109
Last Day
4,504
Last Week
27,756
Last Month
273,888
Last Year
4,297,793
102 Stars
72 Commits
11 Forks
4 Watching
1 Branches
2 Contributors
Latest Version
1.5.1
Package Id
semaphore-async-await@1.5.1
Size
6.09 kB
NPM Version
4.1.2
Node Version
7.6.0
Publised On
20 Mar 2017
Cumulative downloads
Total Downloads
Last day
-25.9%
4,504
Compared to previous day
Last week
-53.9%
27,756
Compared to previous week
Last month
-25.8%
273,888
Compared to previous month
Last year
-20.7%
4,297,793
Compared to previous year
7
A promise-based semaphore implementation suitable to be used with async/await.
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 await criticalFunction(); 7 8 lock.release(); 9}
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 wait(2000); 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 1/29 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 effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
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 Moreasync-sema
Semaphore using `async` and `await`
@algonomia/semaphore-async-await
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.)
@gombosg/semaphore-async-await
A promise-based semaphore implementation suitable to be used with async/await.
async-await-semaphore
async await semaphore implement