Package: effect
Module: Fiber
A runtime fiber is a lightweight thread that executes Effects. Fibers are the unit of concurrency in Effect. They provide a way to run multiple Effects concurrently while maintaining structured concurrency and cancellation safety.
When to use
Use to observe, join, interrupt, or coordinate work that has already been forked.
Details
A fiber exposes both safe Effect-based operations, such as await,
join, and interrupt, and low-level runtime fields used by
the scheduler and runtime internals.
Gotchas
Prefer the exported functions in this module over calling interruptUnsafe
or pollUnsafe directly. The unsafe methods are immediate runtime hooks and
do not provide the same Effect-based sequencing guarantees.
Example (Awaiting a forked fiber)
import { Effect, Fiber } from "effect"
const program = Effect.gen(function*() {
// Fork an effect to run in a new fiber
const fiber = yield* Effect.forkChild(Effect.succeed(42))
// Wait for the fiber to complete and get its result
const result = yield* Fiber.await(fiber)
console.log(result) // Exit.succeed(42)
return result
})
Signature
export interface Fiber<out A, out E = never> extends Pipeable {
readonly [TypeId]: Fiber.Variance<A, E>
readonly id: number
readonly currentOpCount: number
readonly getRef: <A>(ref: Context.Reference<A>) => A
readonly context: Context.Context<never>
setContext(context: Context.Context<never>): void
readonly currentScheduler: Scheduler
readonly currentDispatcher: SchedulerDispatcher
readonly currentSpan?: AnySpan | undefined
readonly currentLogLevel: LogLevel
readonly minimumLogLevel: LogLevel
readonly currentStackFrame?: StackFrame | undefined
readonly maxOpsBeforeYield: number
readonly currentPreventYield: boolean
readonly addObserver: (cb: (exit: Exit<A, E>) => void) => () => void
readonly interruptUnsafe: (
fiberId?: number | undefined,
annotations?: Context.Context<never> | undefined
) => void
readonly pollUnsafe: () => Exit<A, E> | undefined
}
Since v2.0.0