Effect · reference
Reference
The Effect type
A lazy, immutable description. Nothing runs until a run* function interprets it.
Effect<Success, Error, Requirements>
Value produced on success. void = no payload. never = never succeeds (runs forever, or only fails).
Expected, recoverable failures. never = cannot fail that way (no values of never).
Services the runtime must provide. never = empty context; you can run it now.
Abbreviations in the ecosystem: A / E / R. Defaults in TypeScript: omitted E and R are never, so Effect.succeed(42) is Effect<number, never, never>.
What it is not
- Not a function, even though it is useful to imagine
(context) => Error | Success. - Not a running
Promise. Promises are eager and one-shot; Effects are lazy and repeatable. - The value does no I/O. The runtime does.
Construct vs run
import { Effect } from "effect"
const program = Effect.sync(() => {
console.log("Hello, World!")
return 1
})
// still silent
const result = Effect.runSync(program)
// logs, then result === 1
Keep run* at the edge of the program. Default to runPromise or runFork. runSync is for effects you know are synchronous and non-failing; async work inside it throws AsyncFiberException.
| API | Gives |
|---|---|
runSync | A (or throws) |
runSyncExit | Exit<A, E> |
runPromise | Promise<A> |
runPromiseExit | Promise<Exit<A, E>> |
runFork | RuntimeFiber<A, E> |
React boundary (install docs)
Hold the description; run on the event.
const task = useMemo(
() => Effect.sync(() => setCount((n) => n + 1)),
[setCount]
)
const increment = useCallback(() => Effect.runSync(task), [task])
Effect does not replace useState. It describes the work the click performs.