Skip to content

@unthrown/neverthrow


@unthrown/neverthrow

Functions

fromNeverthrow()

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

Defined in: index.ts:83

Convert a neverthrow Result into a Result.

Type Parameters

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

Parameters

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

Returns

Result<T, E>

Remarks

Ok → Ok, Err → Err. neverthrow carries no Defect, so the result is never a Defect.

Example

ts
import { ok } from "neverthrow";
import { fromNeverthrow } from "@unthrown/neverthrow";

const result = fromNeverthrow(ok(1));
result.isOk(); // => true

fromNeverthrowAsync()

ts
function fromNeverthrowAsync<T, E>(resultAsync): AsyncResult<T, E>;

Defined in: index.ts:146

Convert a neverthrow ResultAsync into an AsyncResult.

Type Parameters

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

Parameters

ParameterTypeDescription
resultAsyncResultAsync<T, E>the neverthrow async result to convert.

Returns

AsyncResult<T, E>

Remarks

The async counterpart of fromNeverthrow. A modeled Err stays an Err; an unexpected rejection inside the neverthrow chain becomes a Defect. The returned AsyncResult never throws when awaited.

Example

ts
import { okAsync } from "neverthrow";
import { fromNeverthrowAsync } from "@unthrown/neverthrow";

const result = await fromNeverthrowAsync(okAsync(1));
result.isOk(); // => true

toNeverthrow()

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

Defined in: index.ts:51

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

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

Example

ts
import { fromThrowable } from "unthrown";
import { toNeverthrow } from "@unthrown/neverthrow";

// Mint a Defect, then convert: it has no home in neverthrow, so onDefect folds it into E.
const defective = fromThrowable((): number => {
  throw new Error("boom");
}, (cause, defect) => defect(cause))();
const nt = toNeverthrow(defective, (cause) => `bug: ${String(cause)}`);
nt.isErr(); // => true — the Err carries "bug: Error: boom"

toNeverthrowAsync()

ts
function toNeverthrowAsync<T, E>(asyncResult, onDefect): ResultAsync<T, E>;

Defined in: index.ts:116

Convert an AsyncResult into a neverthrow ResultAsync, 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

ResultAsync<T, E>

Remarks

The async counterpart of toNeverthrow: onDefect is required for the same reason. The AsyncResult is awaited (it never rejects) and each settled Result is converted.

A throwing onDefect surfaces as a rejection of the returned ResultAsync's inner promise — neverthrow's own failure mode; do not throw from triage.

Example

ts
import { fromSafePromise } from "unthrown";
import { toNeverthrowAsync } from "@unthrown/neverthrow";

// 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 result = await toNeverthrowAsync(defective, (cause) => `bug: ${String(cause)}`);
result.isErr(); // => true — the Err carries "bug: Error: boom"

Released under the MIT License.