Overview
functionParallel Execution

race

Runs keyed behaviors and returns the first settled child.

Signature

tsSignature
export function race<const B extends BehaviorRecord>(
behaviors: B,
options?: { concurrency?: number },
): Behavior<RecordInput<B>, RaceOutput<B>, RaceError<B> | EmptyRaceError, RecordContext<B>>;

Imports

tsImport
import { race } from "@/lib/flow/race";
import { race } from "@/lib/flow";

Type Parameters

B

Behavior record type.

Parameters

behaviors
B

Keyed behaviors to race with the same input.

options
{ concurrency?: number }

Optional concurrency limit.

Returns

A behavior producing a keyed success or keyed failure.

Details

On success, the returned value includes the winning key and value. On failure, the returned error

includes the failing key and error. Remaining active children are aborted.

Examples

tsExample
const fastestProfile = race({
primary: loadFromPrimary,
replica: loadFromReplica,
});
Behavior

Executable Flow workflow unit.

type
RecordContext

Intersects the custom context types of every behavior in a record.

type

Source

tsflow/race.ts:42-53
export function race<const B extends BehaviorRecord>(
behaviors: B,
options?: { concurrency?: number },
): Behavior<RecordInput<B>, RaceOutput<B>, RaceError<B> | EmptyRaceError, RecordContext<B>> {
return async (input, context) => {
if (context.signal.aborted) {
return resultFromSignal(context.signal);
}
return runRace(behaviors, input, context, options?.concurrency);
};
}