API reference@evolu/commonTask › race

function race<T>(
  tasks: T,
  __namedParameters?: {
    abortReason?: unknown;
  },
): Task<
  InferTaskOk<T[number]>,
  InferTaskErr<T[number]>,
  InferTaskDeps<T[number]>
>;

Defined in: packages/common/src/Task.ts:1823

Returns a Task that completes first.

Like Promise.race, the first Task to complete (whether success or failure) wins. All other Tasks are aborted. Use any if you need the first Task to succeed instead.

Requires a non-empty array — racing zero Tasks has no meaningful result (there's no "first to complete" without participants). This is enforced at compile time for non-empty tuple types. For other arrays, guard with isNonEmptyArray:

if (isNonEmptyArray(tasks)) {
  await run(race(tasks));
}

Example

const fast: Task<string> = () => ok("fast");
const slow: Task<string> = async (run) => {
  await run(sleep("10ms"));
  return ok("slow");
};

// First wins, others are aborted.
const result = await run(race([fast, slow])); // ok("fast")

Always runs with unlimited concurrency — a sequential race makes no sense since the first Task would always "win".