Function: unwrap()

Extracts the success value from a Result or ResultAsync.

If the input is a Failure, it will throw the error or return the default value if provided.

Type Param

The input Result or ResultAsync.

Type Param

The default value type (optional).

Examples

import { Result } from '@praha/byethrow';

const result = Result.succeed(42);
const value = Result.unwrap(result); // 42
import { Result } from '@praha/byethrow';

const result = Result.succeed(42);
const value = Result.unwrap(result, 0); // 42
import { Result } from '@praha/byethrow';

const result = Result.fail('error');
Result.unwrap(result); // throws 'error'
import { Result } from '@praha/byethrow';

const result = Result.fail('error');
const value = Result.unwrap(result, 0); // 0

See

assertSuccess - When used with assertSuccess, you can safely unwrap the Result.

Call Signature

unwrap<R>(result): true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>

Defined in: functions/unwrap.ts:56

Type Parameters

R

R extends ResultMaybeAsync<any, any>

Parameters

result

R

Returns

true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>

Call Signature

unwrap<R, T>(result, defaultValue): true extends HasPromise<R> ? Promise<T | InferSuccess<R>> : T | InferSuccess<R>

Defined in: functions/unwrap.ts:57

Type Parameters

R

R extends ResultMaybeAsync<any, any>

T

T

Parameters

result

R

defaultValue

T

Returns

true extends HasPromise<R> ? Promise<T | InferSuccess<R>> : T | InferSuccess<R>

Call Signature

unwrap<R>(): (result) => true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>

Defined in: functions/unwrap.ts:58

Type Parameters

R

R extends ResultMaybeAsync<any, any>

Returns

(result): true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>

Parameters

result

R

Returns

true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>

Call Signature

unwrap<R, T>(defaultValue): (result) => true extends HasPromise<R> ? Promise<T | InferSuccess<R>> : T | InferSuccess<R>

Defined in: functions/unwrap.ts:59

Type Parameters

R

R extends ResultMaybeAsync<any, any>

T

T

Parameters

defaultValue

T

Returns

(result): true extends HasPromise<R> ? Promise<T | InferSuccess<R>> : T | InferSuccess<R>

Parameters

result

R

Returns

true extends HasPromise<R> ? Promise<T | InferSuccess<R>> : T | InferSuccess<R>