Skip to content

Latest commit

 

History

History
25 lines (19 loc) · 1.29 KB

README.md

File metadata and controls

25 lines (19 loc) · 1.29 KB

semaphore

Semaphore for TypeScript/JavaScript.

npmjs Language: TypeScript Prettier eslint Code Style: Google

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"