Skip to content

@unthrown/effect


@unthrown/effect

Functions

fromEffect()

ts
function fromEffect<T, E>(effect): AsyncResult<T, E>;

Defined in: index.ts:165

Run an Effect and collect its outcome as an AsyncResult.

Type Parameters

Type ParameterDescription
Tthe success value type.
Ethe modeled error type.

Parameters

ParameterTypeDescription
effectEffect<T, E>the effect to run.

Returns

AsyncResult<T, E>

Remarks

The effect must need no environment (R = never). It is run to an Exit (which never rejects), then folded with fromExit: success → Ok, a modeled failure → Err, a die/interruption → Defect. The returned AsyncResult never throws when awaited.


fromEither()

ts
function fromEither<T, E>(either): Result<T, E>;

Defined in: index.ts:120

Convert an Effect Either into a Result.

Type Parameters

Type ParameterDescription
Tthe success value type.
Ethe modeled error type.

Parameters

ParameterTypeDescription
eitherEither<T, E>the either to convert.

Returns

Result<T, E>

Remarks

Right → Ok, Left → Err. An Either carries no defect, so the result is never a Defect.


fromExit()

ts
function fromExit<T, E>(exit): Result<T, E>;

Defined in: index.ts:70

Convert an Effect Exit into a Result — the inverse of toExit.

Type Parameters

Type ParameterDescription
Tthe success value type.
Ethe modeled error type.

Parameters

ParameterTypeDescription
exitExit<T, E>the exit to convert.

Returns

Result<T, E>

Remarks

Exit.Success → Ok. For a failure, the enclosing Cause is reduced:

  • a Cause.die becomes a Defect,
  • otherwise a Cause.fail becomes the modeled Err,
  • a pure interruption (or empty cause) becomes a Defect.

A Defect dominates a modeled failure in a composite cause — the same rule unthrown's all uses, on the principle that an unexpected failure is the more severe signal.


toEffect()

Call Signature

ts
function toEffect<T, E>(source): Effect<T, E>;

Defined in: index.ts:140

Lift a Result or AsyncResult into an Effect.

Type Parameters
Type ParameterDescription
Tthe success value type.
Ethe modeled error type.
Parameters
ParameterTypeDescription
sourceResult<T, E>the result, or async result, to lift.
Returns

Effect<T, E>

Remarks

Ok → Effect.succeed, Err → Effect.fail, Defect → Effect.die. The resulting Effect needs no environment (R = never). An AsyncResult is awaited inside the effect (it never rejects), so this is the AsyncResult → Effect direction too.

Call Signature

ts
function toEffect<T, E>(source): Effect<T, E>;

Defined in: index.ts:141

Lift a Result or AsyncResult into an Effect.

Type Parameters
Type ParameterDescription
Tthe success value type.
Ethe modeled error type.
Parameters
ParameterTypeDescription
sourceAsyncResult<T, E>the result, or async result, to lift.
Returns

Effect<T, E>

Remarks

Ok → Effect.succeed, Err → Effect.fail, Defect → Effect.die. The resulting Effect needs no environment (R = never). An AsyncResult is awaited inside the effect (it never rejects), so this is the AsyncResult → Effect direction too.


toEither()

ts
function toEither<T, E>(result, onDefect): Either<T, E>;

Defined in: index.ts:98

Convert a Result into an Effect Either, triaging any defect.

Type Parameters

Type ParameterDescription
Tthe success value type.
Ethe modeled error type.

Parameters

ParameterTypeDescription
resultResult<T, E>the result to convert.
onDefect(cause) => Efolds a defect's unknown cause into a modeled E.

Returns

Either<T, E>

Remarks

Either has no defect channel, so a Defect cannot pass through silently — onDefect must fold its cause into a modeled error E (a Left). This is the boundary-qualification rule (Thesis #3) applied on the way out: Ok → Right, Err → Left, Defect → Left(onDefect(cause)).


toExit()

ts
function toExit<T, E>(result): Exit<T, E>;

Defined in: index.ts:43

Convert a Result into an Effect Exit — a bijection, since both carry three channels.

Type Parameters

Type ParameterDescription
Tthe success value type.
Ethe modeled error type.

Parameters

ParameterTypeDescription
resultResult<T, E>the result to convert.

Returns

Exit<T, E>

Remarks

Ok → Exit.succeed, Err → Exit.fail (a modeled Cause.fail), and Defect → Exit.die (an unexpected Cause.die). Round-trips with fromExit.

Example

ts
import { ok } from "unthrown";
import { toExit } from "@unthrown/effect";
toExit(ok(1)); // Exit.succeed(1)

Released under the MIT License.