Skip to content

Result

Result: object

Defined in: packages/core/src/result.ts:47

Result utility functions for chaining and transformation

expect<T, E>(result, message): T

Return the value on success, or throw with a custom message on failure. Use this when you want to convert a failed Result to an exception.

T

E

Result<T, E>

The Result to expect

string

Custom error message to use if result is fail

T

The success value

Error with the provided message (and original error as cause)

const value = Result.expect(result, 'Message send failed');
// throws Error('Message send failed') if result is fail

flatMap<T, U, E>(result, fn): Result<U, E>

Chain Result-returning operations

T

U

E

Result<T, E>

(value) => Result<U, E>

Result<U, E>

fromPromise<T, E>(promise): Promise<Result<T, E>>

Convert a Promise to a Result

T

E = Error

Promise<T>

Promise<Result<T, E>>

isFail<T, E>(result): result is Fail<E>

Check if a Result is Fail

T

E

Result<T, E>

result is Fail<E>

isOk<T, E>(result): result is Ok<T>

Check if a Result is Ok

T

E

Result<T, E>

result is Ok<T>

map<T, U, E>(result, fn): Result<U, E>

Transform the success value of a Result

T

U

E

Result<T, E>

(value) => U

Result<U, E>

mapError<T, E, F>(result, fn): Result<T, F>

Transform the error of a Result

T

E

F

Result<T, E>

(error) => F

Result<T, F>

match<T, E, U>(result, handlers): U

Pattern match on a Result

T

E

U

Result<T, E>

(error) => U

(value) => U

U

tap<T, E>(result, fn): Result<T, E>

Execute a side-effect without breaking the chain. Calls fn with the result (ok or fail) and returns the same result.

T

E

Result<T, E>

The Result to tap

(result) => void

Side-effect function called with the result

Result<T, E>

The same Result for chaining

const result = await provider.send(options);
Result.tap(result, r => console.log('Completed:', r));

tapErr<T, E>(result, fn): Result<T, E>

Execute a side-effect on failure only. Calls fn with the error only if result is fail, returns the same result.

T

E

Result<T, E>

The Result to tap

(error) => void

Side-effect function called with the error on failure

Result<T, E>

The same Result for chaining

Result.tapErr(result, error => console.error('Failed:', error.message));

tapOk<T, E>(result, fn): Result<T, E>

Execute a side-effect on success only. Calls fn with the value only if result is ok, returns the same result.

T

E

Result<T, E>

The Result to tap

(value) => void

Side-effect function called with the value on success

Result<T, E>

The same Result for chaining

Result.tapOk(result, value => console.log('Success:', value.messageId));

unwrap<T, E>(result): T

Extract the value or throw the error

T

E

Result<T, E>

T

unwrapOr<T, E>(result, defaultValue): T

Extract the value or return a default

T

E

Result<T, E>

T

T

unwrapOrElse<T, E>(result, fn): T

Extract the value or compute a default from the error

T

E

Result<T, E>

(error) => T

T