Skip to content

@unthrown/boxed


@unthrown/boxed

Functions

fromBoxed()

ts
function fromBoxed<T, E>(result): Result<T, E>;

Defined in: index.ts:78

Convert a Boxed Result into a Result.

Type Parameters

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

Parameters

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

Returns

Result<T, E>

Remarks

Result.Ok → Ok, Result.Error → Err. Boxed's Result carries no Defect, so the result is never a Defect.

Example

ts
import { Result as BoxedResult } from "@bloodyowl/boxed";
import { fromBoxed } from "@unthrown/boxed";

const result = fromBoxed(BoxedResult.Ok(1));
result.isOk(); // => true

fromBoxedFuture()

ts
function fromBoxedFuture<T, E>(future): AsyncResult<T, E>;

Defined in: index.ts:164

Convert a Boxed Future<Result> into an AsyncResult.

Type Parameters

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

Parameters

ParameterTypeDescription
futureFuture<Result<T, E>>the Boxed future to convert.

Returns

AsyncResult<T, E>

Remarks

The async counterpart of fromBoxed. A Result.Error inside the future stays an Err. Boxed's Future has no failure channel of its own — Future.toPromise() does not reject — so in practice no Defect arises here; the fromSafePromise boundary is a defensive net that would only capture one if a future somehow rejected. The returned AsyncResult never throws when awaited.

Example

ts
import { Future, Result as BoxedResult } from "@bloodyowl/boxed";
import { fromBoxedFuture } from "@unthrown/boxed";

const result = await fromBoxedFuture(Future.value(BoxedResult.Ok(1)));
result.isOk(); // => true

toBoxed()

ts
function toBoxed<T, E>(result, onDefect): Result<T, E>;

Defined in: index.ts:46

Convert a Result into a Boxed Result, 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

Result<T, E>

Remarks

Boxed's Result has no Defect channel, so onDefect must fold a Defect's cause into a modeled error E (an Error). Ok → Result.Ok, Err → Result.Error, Defect → Result.Error(onDefect(cause)).

Example

ts
import { fromThrowable } from "unthrown";
import { toBoxed } from "@unthrown/boxed";

// Mint a Defect, then convert: it has no home in Boxed's Result, so onDefect folds it into E.
const defective = fromThrowable((): number => {
  throw new Error("boom");
}, (cause, defect) => defect(cause))();
const boxed = toBoxed(defective, (cause) => `bug: ${String(cause)}`);
boxed.isError(); // => true — the Error carries "bug: Error: boom"

toBoxedFuture()

ts
function toBoxedFuture<T, E>(asyncResult, onDefect): Future<Result<T, E>>;

Defined in: index.ts:114

Convert an AsyncResult into a Boxed Future<Result>, triaging any Defect.

Type Parameters

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

Parameters

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

Returns

Future<Result<T, E>>

Remarks

The async counterpart of toBoxed: onDefect is required for the same reason. The AsyncResult is awaited (it never rejects) and its settled Result is converted, then resolved into the Future.

onDefect must not throw: Boxed's Future has no failure channel, so a throw is re-raised out-of-band (uncaught) rather than left as a hung Future.

Example

ts
import { fromSafePromise } from "unthrown";
import { toBoxedFuture } from "@unthrown/boxed";

// A rejection inside fromSafePromise is a Defect; onDefect folds it into E on the way out.
const defective = fromSafePromise(Promise.reject(new Error("boom")));
const future = toBoxedFuture(defective, (cause) => `bug: ${String(cause)}`);
(await future.toPromise()).isError(); // => true — the Error carries "bug: Error: boom"

Released under the MIT License.