Overview
functionCore Runtime

runBehavior

Executes a behavior with runtime error protection.

Signature

tsSignature
export async function runBehavior<I, O, E, C extends object>(
behavior: Behavior<I, O, E, C>,
input: I,
context: RuntimeContext<C>,
): Promise<Result<O, E | RuntimeError>>;

Imports

tsImport
import { runBehavior } from "@/lib/flow/core/runtime";
import { runBehavior } from "@/lib/flow";

Type Parameters

I

Input type.

O

Output type.

E

Expected domain error type.

C

Custom context type.

Parameters

behavior
Behavior<I, O, E, C>

Behavior to execute.

input
I

Input passed to the behavior.

context

Runtime context containing an `AbortSignal`.

Returns

The behavior result, cancellation result, or thrown runtime result.

Return type
Promise<Result<O, E | RuntimeError>>

Details

The runner checks cancellation before and after behavior execution and converts thrown

exceptions into `Thrown` results.

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
RuntimeError

Runtime-level failure union added around every behavior's domain error type.

type
thrown

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

re-export
Thrown

Runtime error returned when user code throws.

type

Source

tsflow/core/runtime.ts:107-131
export async function runBehavior<I, O, E, C extends object>(
behavior: Behavior<I, O, E, C>,
input: I,
context: RuntimeContext<C>,
): Promise<Result<O, E | RuntimeError>> {
if (context.signal.aborted) {
return resultFromSignal(context.signal);
}
try {
const result = await behavior(input, context);
if (context.signal.aborted) {
return resultFromSignal(context.signal);
}
return result;
} catch (error) {
if (context.signal.aborted) {
return resultFromSignal(context.signal);
}
return thrown(error);
}
}