Skip to content

hortemo/semaphore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@hortemo/semaphore

Promise-based FIFO semaphore for JavaScript and TypeScript.

Install

npm install @hortemo/semaphore

Use

import Semaphore from "@hortemo/semaphore";

const semaphore = new Semaphore(5);

async function processItem(item: string): Promise<void> {
  const release = await semaphore.acquire();
  try {
    await doWork(item);
  } finally {
    release();
  }
}

await Promise.all(items.map(processItem));

API

  • new Semaphore(permits: number): Create a semaphore with permits available.
  • await semaphore.acquire(): Wait for a permit and receive a Releaser. Call it to free the permit.
  • type Releaser = () => void

About

Promise-based FIFO semaphore for JavaScript and TypeScript.

Resources

License

Stars

Watchers

Forks

Packages

No packages published