@unthrown/neverthrow
@unthrown/neverthrow
Functions
fromNeverthrow()
function fromNeverthrow<T, E>(result): Result<T, E>;Defined in: index.ts:83
Convert a neverthrow Result into a Result.
Type Parameters
| Type Parameter | Description |
|---|---|
T | the success value type. |
E | the modeled error type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
result | Result<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
import { ok } from "neverthrow";
import { fromNeverthrow } from "@unthrown/neverthrow";
const result = fromNeverthrow(ok(1));
result.isOk(); // => truefromNeverthrowAsync()
function fromNeverthrowAsync<T, E>(resultAsync): AsyncResult<T, E>;Defined in: index.ts:146
Convert a neverthrow ResultAsync into an AsyncResult.
Type Parameters
| Type Parameter | Description |
|---|---|
T | the success value type. |
E | the modeled error type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
resultAsync | ResultAsync<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
import { okAsync } from "neverthrow";
import { fromNeverthrowAsync } from "@unthrown/neverthrow";
const result = await fromNeverthrowAsync(okAsync(1));
result.isOk(); // => truetoNeverthrow()
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 Parameter | Description |
|---|---|
T | the success value type. |
E | the modeled error type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
result | Result<T, E> | the result to convert. |
onDefect | (cause) => E | folds 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
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()
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 Parameter | Description |
|---|---|
T | the success value type. |
E | the modeled error type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
asyncResult | AsyncResult<T, E> | the async result to convert. |
onDefect | (cause) => E | folds 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
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"