Genel bakış
functionComposition

task

Creates a typed behavior from synchronous or asynchronous application code.

Signature

tsSignature
export function task<I, O, E = never, C extends object = {}>(
run: (input: I, context: TaskContext<C>) => Awaitable<TaskReturn<O, E>>,
): Behavior<I, O, E, C>;

Imports

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

Type Parameters

I

Input type.

O

Output type.

E

Expected domain error type.

C

Custom context type.

Parameters

run
(input: I, context: TaskContext<C>) => Awaitable<TaskReturn<O, E>>

Callback invoked for each behavior execution.

Returns

A behavior that wraps plain values in `ok` and preserves explicit results.

Return type
Behavior<I, O, E, C>

Details

`task` is the main authoring boundary for Flow programs. Return plain values for success or use

`context.fail`/`context.err` for typed domain failures. Thrown exceptions are captured by the

runtime as `Thrown` errors.

Examples

tsExample
const loadUser = task<string, { id: string }, { kind: "missing" }>((id, context) => {
if (!id) return context.fail({ kind: "missing" });
return { id };
});
Awaitable

Represents a value that may be returned directly or through a Promise.

type
Behavior

Executable Flow workflow unit.

type
err

Re-exports core result helpers used by advanced record internals.

re-export
fail

Creates a behavior that always fails with the same domain error.

function
input

Creates an identity behavior for starting typed pipelines.

function
run

Runs a behavior with an optional application context.

function
Thrown

Runtime error returned when user code throws.

type

Source

tsflow/task.ts:51-65
export function task<I, O, E = never, C extends object = {}>(
run: (input: I, context: TaskContext<C>) => Awaitable<TaskReturn<O, E>>,
): Behavior<I, O, E, C> {
return define(async (input: I, context: RuntimeContext<C>) => {
const result = await run(input, {
...context,
ok,
succeed: ok,
fail: err,
err,
});
return isResult(result) ? result : ok(result);
});
}