Semaphore for TypeScript/JavaScript.
A Semaphore implementation clone of Python's asyncio.Semaphore
.
A semaphore manages an internal counter which is decremented by each acquire() call and incremented by each release() call. The counter can never go below zero; when acquire() finds that it is zero, it blocks, waiting until some other thread calls release().
As JavaScript does not support context managers, you will need to wrap your function with the run()
method.
const semaphore = new Semaphore(10);
async function myFunction() {
// some async operation
return 'Done';
}
const result = await semaphore.run(() => myFunction());
console.log(result); // prints "Done"