Skip to content

RoundRobin

Defined in: packages/async/src/synchro/round-robin.ts:26

Hands one turn at a time to a fixed set of participants, cyclically.

Every participant takes a turn before any one of them takes a second, and turns never overlap — the next holder is only woken once the previous one has released it. A participant that finishes early leaves, and the remaining ones carry on without it, still strictly one at a time.

const turns = new RoundRobin(3);
async function worker(id: number, batches: number) {
await turns.take(id);
try {
for (let batch = 0; batch < batches; batch++) {
// ...do this worker's share of one round...
if (batch < batches - 1) {
await turns.next(id);
}
}
} finally {
turns.leave(id);
}
}

new RoundRobin(participants): RoundRobin

Defined in: packages/async/src/synchro/round-robin.ts:38

number

How many fixed, 0-based participant slots to cycle through.

RoundRobin

get maxRounds(): number

Defined in: packages/async/src/synchro/round-robin.ts:95

Turns taken by the participant that took the most.

number


get minRounds(): number

Defined in: packages/async/src/synchro/round-robin.ts:107

Turns taken by the participant that took the fewest.

The rounds up to this point are the only ones every participant took part in: past it, at least one participant had already left, so whatever ran the turn alongside it did so under different conditions (less contention, different concurrent load).

number

leave(id): void

Defined in: packages/async/src/synchro/round-robin.ts:84

Gives up this participant’s turn for good.

number

void


next(id): Promise<void>

Defined in: packages/async/src/synchro/round-robin.ts:63

Passes the turn on, and waits for it to come back.

number

Promise<void>


take(id): Promise<void>

Defined in: packages/async/src/synchro/round-robin.ts:52

Resolves when it is this participant’s turn.

number

0-based participant index, in turn order.

Promise<void>