Skip to content

@unthrown/neverthrow


@unthrown/neverthrow

Functions

fromNeverthrow()

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

Defined in: index.ts:60

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.


fromNeverthrowAsync()

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

Defined in: index.ts:99

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.


toNeverthrow()

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

Defined in: index.ts:38

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)).


toNeverthrowAsync()

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

Defined in: index.ts:78

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.

Released under the MIT License.