Skip to content

Barrier

Defined in: packages/async/src/synchro/barrier.ts:26

An asynchronous reusable barrier for N participants.

Barriers allow multiple asynchronous tasks to wait until a specified number of them have reached a certain point in their execution.

After the required number of tasks have called wait(), all waiting tasks are released, and the barrier resets for reuse.

const barrier = new Barrier(3);
async function task(id: number) {
console.log(`Task ${id} is waiting at the barrier.`);
await barrier.wait();
console.log(`Task ${id} has crossed the barrier.`);
}
// Start 3 tasks that will wait at the barrier
task(1);
task(2);
task(3);

new Barrier(count): Barrier

Defined in: packages/async/src/synchro/barrier.ts:34

Create a Barrier for the specified number of participants.

number

Number of participants required to release the barrier.

Barrier

wait(signal?): Promise<void>

Defined in: packages/async/src/synchro/barrier.ts:48

Wait for the barrier.

After releasing all waiters, the barrier resets for reuse.

AbortSignal | null

An optional AbortSignal to cancel the wait operation.

Promise<void>

A promise that resolves when the barrier is released.