diff --git a/lib/all.ts b/lib/all.ts index 7aa26fef..c8d32e0c 100644 --- a/lib/all.ts +++ b/lib/all.ts @@ -1,4 +1,4 @@ -import type { Operation, Task } from "./types.ts"; +import type { Operation, Task, Yielded } from "./types.ts"; import { spawn } from "./instructions.ts"; import { call } from "./call.ts"; @@ -52,12 +52,3 @@ export function all[] | []>( type All[] | []> = { -readonly [P in keyof T]: Yielded; }; - -/** - * Unwrap the type of an `Operation`. - * - * Yielded> === T - */ -type Yielded> = T extends Operation - ? TYield - : never; diff --git a/lib/race.ts b/lib/race.ts index baced4dd..e7285f63 100644 --- a/lib/race.ts +++ b/lib/race.ts @@ -1,4 +1,4 @@ -import type { Operation } from "./types.ts"; +import type { Operation, Yielded } from "./types.ts"; import { action, spawn } from "./instructions.ts"; /** @@ -21,16 +21,15 @@ import { action, spawn } from "./instructions.ts"; * }); * ``` * - * @typeParam T the type of the operations that race against each other * @param operations a list of operations to race against each other * @returns the value of the fastest operation */ -export function race(operations: Operation[]): Operation { - return action(function* (resolve, reject) { +export function race>(operations: readonly T[]): Operation> { + return action>(function* (resolve, reject) { for (let operation of operations) { yield* spawn(function* () { try { - resolve(yield* operation); + resolve((yield* operation) as Yielded); } catch (error) { reject(error); } diff --git a/lib/types.ts b/lib/types.ts index 7872e564..f0c13d00 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -303,3 +303,12 @@ export interface Frame extends Computation> { crash(error: Error): Computation>; destroy(): Computation>; } + +/** + * Unwrap the type of an `Operation`. + * + * Yielded> === T + */ +export type Yielded> = T extends Operation + ? TYield + : never;