Creates a new Semaphore with the given capacity.
Semaphore
The maximum number of concurrent operations allowed. Must be a non-negative integer. Use 0 to disallow any concurrent acquire (acquire will wait forever).
0
A new Semaphore instance.
If permits is negative or not an integer.
permits
// Limit to 5 concurrent operationsconst sem = Semaphore(5);// Binary semaphore (equivalent to a value-less Mutex)const binary = Semaphore(1); Copy
// Limit to 5 concurrent operationsconst sem = Semaphore(5);// Binary semaphore (equivalent to a value-less Mutex)const binary = Semaphore(1);
// Task queue: process 2 jobs at a timeconst sem = Semaphore(2);async function processJob(job: Job) { return sem.withPermit(async () => { return await runJob(job); });}await Promise.all(jobs.map(processJob)); Copy
// Task queue: process 2 jobs at a timeconst sem = Semaphore(2);async function processJob(job: Job) { return sem.withPermit(async () => { return await runJob(job); });}await Promise.all(jobs.map(processJob));
Creates a new
Semaphorewith the given capacity.