await-semaphore
Awaitable semaphore/mutex
A semaphore implementation using ES6 promises and supporting 3 styles:
- async/await style (needs typescript)
- thunk style (automatic acquire/release)
- promise style
Also includes Mutex
as a convenience for new Semaphore(1)
.
API
new Semaphore(count: number)
Create a new semaphore with the given count.
; var semaphore = 10;
semaphore.acquire(): Promise<() => void>
Acquire the semaphore and returns a promise for the release function. Be sure to handle release for exception case.
semaphore;
semaphore.use(thunk: () => Promise): Promise
Alternate method for using the semaphore by providing a thunk. This automatically handles acquire/release.
semaphore;
new Mutex()
An alias for new Semaphore(1)
. Mutex has the same methods as Semaphore.
; var mutex = ;
Examples
Create a version of fetch()
with concurrency limited to 10.
async/await style (typescript)
;
thunk style (javascript)
var semaphore = 10; { return semaphore;}
promise style (javascript)
var semaphore = 10; { return semaphore ;}