@unthrown/effect
v5.11.0
Published
Effect interop for unthrown
Maintainers
Readme
@unthrown/effect
📖 Documentation · API Reference
pnpm add @unthrown/effect effect unthrownEffect is the one neighbour that shares unthrown's three-channel shape: an
Exit<A, E> is a success or a Cause, and a Cause distinguishes a modeled
failure (Cause.fail ↔ Err) from an unexpected one (Cause.die ↔ Defect).
So Result ↔ Exit is a genuine bijection.
import { Ok, Err, P, TaggedError } from "unthrown";
import { toExit, fromEffect, toEither } from "@unthrown/effect";
import { Effect } from "effect";
class NotFound extends TaggedError("NotFound") {}
toExit(Ok(1)); // Exit.succeed(1)
toExit(Err("e")); // Exit.fail("e") — a modeled Cause.fail
const load = (id: string): Effect.Effect<string, NotFound> =>
id === "1" ? Effect.succeed("value") : Effect.fail(new NotFound());
// Run an Effect and collect its outcome (die/interrupt become a Defect):
await fromEffect(load("1")).match({
ok: (value) => value,
// the error channel, matched exhaustively — every case named
errCases: (matcher) => matcher.with(P.tag("NotFound"), () => "missing"),
defect: String,
});toExit/fromExit— the bijection:Ok↔succeed,Err↔Cause.fail,Defect↔Cause.die. On the way back a die/interruption becomes aDefect, and aDefectdominates a modeled failure in a composite cause (same rule asall).toEither/fromEither—Eitherhas no defect channel, sotoEither(r, onDefect)forces you to triage the defect intoE(Thesis #3).fromEithernever yields aDefect.toEffect/fromEffect—toEffectlifts aResultorAsyncResultinto anEffect<T, E>(Defect → Effect.die);fromEffectruns an environment-freeEffect<T, E>to anAsyncResult<T, E>.
effect is a peer dependency.
License
MIT © Benoit TRAVERS
