Genel bakış
functionCore Runtime

define

Defines a behavior from a callback that returns a `Result`.

Signature

tsSignature
export function define<I, O, E = unknown, C extends object = {}>(
run: (input: I, context: RuntimeContext<C>) => Awaitable<Result<O, E>>,
): Behavior<I, O, E, C>;

Imports

tsImport
import { define } from "@/lib/flow/core/runtime";
import { define } 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: RuntimeContext<C>) => Awaitable<Result<O, E>>

Callback invoked for each behavior execution.

Returns

A behavior protected by cancellation and thrown-exception handling.

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

Details

Use `define` for low-level helpers that already operate in the result channel. Most application

code should use `task`, which also accepts plain successful values.

Awaitable

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

type
Behavior

Executable Flow workflow unit.

type
input

Creates an identity behavior for starting typed pipelines.

function
Result

Explicit success-or-failure outcome used throughout Flow.

type
run

Runs a behavior with an optional application context.

function
task

Creates a typed behavior from synchronous or asynchronous application code.

function

Source

tsflow/core/runtime.ts:146-170
export function define<I, O, E = unknown, C extends object = {}>(
run: (input: I, context: RuntimeContext<C>) => Awaitable<Result<O, E>>,
): Behavior<I, O, E, C> {
return async (input, context) => {
if (context.signal.aborted) {
return resultFromSignal(context.signal);
}
try {
const result = await run(input, context);
if (context.signal.aborted) {
return resultFromSignal(context.signal);
}
return result;
} catch (error) {
if (context.signal.aborted) {
return cancelled(context.signal.reason);
}
return thrown(error);
}
};
}