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.
Example
Section titled “Example”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);Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Latch(
count):Latch
Defined in: packages/async/src/synchro/latch.ts:41
Create a Latch for the specified number of participants.
Parameters
Section titled “Parameters”number
Number of countDown calls required to release the latch.
Returns
Section titled “Returns”Latch
Accessors
Section titled “Accessors”released
Section titled “released”Get Signature
Section titled “Get Signature”get released():
boolean
Defined in: packages/async/src/synchro/latch.ts:50
Returns true if the latch has been released.
Returns
Section titled “Returns”boolean
Methods
Section titled “Methods”countDown()
Section titled “countDown()”countDown():
void
Defined in: packages/async/src/synchro/latch.ts:66
Decrement the latch. When the count reaches zero, all waiters are released.
Returns
Section titled “Returns”void
wait()
Section titled “wait()”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.
Parameters
Section titled “Parameters”signal?
Section titled “signal?”AbortSignal | null
An optional AbortSignal to cancel the wait operation.
Returns
Section titled “Returns”Promise<void>
A promise that resolves when the latch is released.