Skip to content

Condition

Defined in: packages/async/src/synchro/condition.ts:31

A condition variable primitive.

Allows tasks to wait until they are signaled to continue.

const mutex = new Mutex();
const condition = new Condition();
let ready = false;
// Task 1
async function task1() {
await mutex.lock();
while (!ready) {
await condition.wait(mutex);
}
// Proceed with task
mutex.unlock();
}
// Task 2
async function task2() {
ready = true;
condition.signal(); // or condition.broadcast() to wake all waiting tasks
}

new Condition(): Condition

Condition

broadcast(): void

Defined in: packages/async/src/synchro/condition.ts:40

Broadcast to all waiting tasks, if any.

void


signal(): void

Defined in: packages/async/src/synchro/condition.ts:35

Signal one waiting task, if any.

void


wait(lock, signal?): Promise<void>

Defined in: packages/async/src/synchro/condition.ts:51

Wait until the condition is signaled.

ILock

The lockable (mutex) to use for synchronization.

AbortSignal | null

An optional AbortSignal to cancel the wait operation.

Promise<void>

A promise that resolves when the condition is signaled.