Skip to content

Latch

Defined in: packages/async/src/synchro/latch.ts:33

An asynchronous reusable latch for N participants.

All waiters are released together when the latch is released (countdown reaches zero).

After releasing all waiters, the latch remains released.

const latch = new Latch(3);
async function task(id: number) {
console.log(`Task ${id} is waiting at the latch.`);
await latch.wait();
console.log(`Task ${id} has crossed the latch.`);
}
// Start 3 tasks that will wait at the latch
task(1);
task(2);
task(3);
// Simulate some work before counting down
setTimeout(() => {
console.log("Counting down the latch");
latch.countDown();
latch.countDown();
latch.countDown();
}, 1000);

new Latch(count): Latch

Defined in: packages/async/src/synchro/latch.ts:41

Create a Latch for the specified number of participants.

number

Number of countDown calls required to release the latch.

Latch

get released(): boolean

Defined in: packages/async/src/synchro/latch.ts:50

Returns true if the latch has been released.

boolean

countDown(): void

Defined in: packages/async/src/synchro/latch.ts:66

Decrement the latch. When the count reaches zero, all waiters are released.

void


wait(signal?): Promise<void>

Defined in: packages/async/src/synchro/latch.ts:61

Wait for the latch to be released. Resolves when countDown() has been called count times.

AbortSignal | null

An optional AbortSignal to cancel the wait operation.

Promise<void>

A promise that resolves when the latch is released.