Promise based semaphore library
npm install semaphore-promisePromise based, TypeScript compatible semaphore library.
``js
const Semaphore = require( 'semaphore-promise' );
const semaphore = new Semaphore( 1 );
semaphore.acquire().then( ( release ) => {
// Do something exclusive
release();
} );
`
This library can be used in TypeScript files as well.
`typescript`
import { Semaphore } from 'semaphore-promise';
Creates a new semaphore object with count semaphores.
opts can contain …
* a name for the semaphore which is used by the logger.logger
* a like pino to log debug messages.
If no logger is provided, nothing is logged.
$3
Resolves as soon as a semaphore could be acquired. Waiting callers are treated in FIFO order.
Returns a release function which is needed to release() the semaphore again.
$3
If a semaphore is available, acquire it and return a release* function.
* Else, throw an Error.
* v1.4.3 (2025-06-10)
* Dev dependencies updated
* v1.4.2 (2023-07-09)
* Options in SemaphoreOptions now have source code comments in the generated .d.ts file
* tryAcquire is now documentedimport Semaphore from 'semaphore-promise'
* TS sources are now included in the Source Map
* v1.4.0 (2022-05-06)
* Add logging capabilities to simplify debugging issues like deadlocks.
* Add names to semaphore, also for debugging.
* v1.3.0 (2022-02-10)
* Add default export so can be used (without curly braces)tryAcquire()`
* Default to 1 semaphore in the constructor
* Docs: Add note on FIFO order
* v1.2.0 (2019-08-07)
* Add