Skip to main content
Module

x/async/mod.ts>Semaphore

🦕 Asynchronous primitive modules for Deno.
Latest
class Semaphore
import { Semaphore } from "https://deno.land/x/async@v2.1.0/mod.ts";

A semaphore that allows a limited number of concurrent executions of an operation.

import { Semaphore } from "https://deno.land/x/async@v2.1.0/semaphore.ts";

const sem = new Semaphore(5);
const worker = () => {
  return sem.lock(async () => {
    // do something
  });
};
await Promise.all([...Array(10)].map(() => worker()));

Constructors

new
Semaphore(size: number)

Creates a new semaphore with the specified limit.

Properties

readonly
locked: boolean

Returns true if the semaphore is currently locked.

Methods

lock<R>(f: () => R | PromiseLike<R>): Promise<R>

Acquires a lock on the semaphore, and invokes the specified function.