@rosen-bridge/semaphore
v0.1.0
Published
A promise-based Semaphore utility to limit async concurrency
Readme
@rosen-bridge/semaphore
Table of contents
Introduction
This package provides a robust Semaphore class that queues up tasks and
ensures that no more than a configured number of tasks (maxConcurrency) run
simultaneously.
Installation
npm:
npm i @rosen-bridge/semaphoreUsage
import { Semaphore } from '@rosen-bridge/semaphore';
const pool = new Semaphore(2);
async function myTestFn(id: number) {
return pool.use(async () => {
await new Promise((res) => setTimeout(res, 1000));
return `${id}`;
});
}
[1, 2, 3, 4, 5].forEach((id) => myTestFn(id));