Result
Result:
object
Defined in: packages/core/src/result.ts:47
Result utility functions for chaining and transformation
Type Declaration
Section titled “Type Declaration”expect()
Section titled “expect()”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.
Type Parameters
Section titled “Type Parameters”T
E
Parameters
Section titled “Parameters”result
Section titled “result”Result<T, E>
The Result to expect
message
Section titled “message”string
Custom error message to use if result is fail
Returns
Section titled “Returns”T
The success value
Throws
Section titled “Throws”Error with the provided message (and original error as cause)
Example
Section titled “Example”const value = Result.expect(result, 'Message send failed');// throws Error('Message send failed') if result is failflatMap()
Section titled “flatMap()”flatMap<
T,U,E>(result,fn):Result<U,E>
Chain Result-returning operations
Type Parameters
Section titled “Type Parameters”T
U
E
Parameters
Section titled “Parameters”result
Section titled “result”Result<T, E>
(value) => Result<U, E>
Returns
Section titled “Returns”Result<U, E>
fromPromise()
Section titled “fromPromise()”fromPromise<
T,E>(promise):Promise<Result<T,E>>
Convert a Promise to a Result
Type Parameters
Section titled “Type Parameters”T
E = Error
Parameters
Section titled “Parameters”promise
Section titled “promise”Promise<T>
Returns
Section titled “Returns”Promise<Result<T, E>>
isFail()
Section titled “isFail()”isFail<
T,E>(result):result is Fail<E>
Check if a Result is Fail
Type Parameters
Section titled “Type Parameters”T
E
Parameters
Section titled “Parameters”result
Section titled “result”Result<T, E>
Returns
Section titled “Returns”result is Fail<E>
isOk()
Section titled “isOk()”isOk<
T,E>(result):result is Ok<T>
Check if a Result is Ok
Type Parameters
Section titled “Type Parameters”T
E
Parameters
Section titled “Parameters”result
Section titled “result”Result<T, E>
Returns
Section titled “Returns”result is Ok<T>
map<
T,U,E>(result,fn):Result<U,E>
Transform the success value of a Result
Type Parameters
Section titled “Type Parameters”T
U
E
Parameters
Section titled “Parameters”result
Section titled “result”Result<T, E>
(value) => U
Returns
Section titled “Returns”Result<U, E>
mapError()
Section titled “mapError()”mapError<
T,E,F>(result,fn):Result<T,F>
Transform the error of a Result
Type Parameters
Section titled “Type Parameters”T
E
F
Parameters
Section titled “Parameters”result
Section titled “result”Result<T, E>
(error) => F
Returns
Section titled “Returns”Result<T, F>
match()
Section titled “match()”match<
T,E,U>(result,handlers):U
Pattern match on a Result
Type Parameters
Section titled “Type Parameters”T
E
U
Parameters
Section titled “Parameters”result
Section titled “result”Result<T, E>
handlers
Section titled “handlers”(error) => U
(value) => U
Returns
Section titled “Returns”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.
Type Parameters
Section titled “Type Parameters”T
E
Parameters
Section titled “Parameters”result
Section titled “result”Result<T, E>
The Result to tap
(result) => void
Side-effect function called with the result
Returns
Section titled “Returns”Result<T, E>
The same Result for chaining
Example
Section titled “Example”const result = await provider.send(options);Result.tap(result, r => console.log('Completed:', r));tapErr()
Section titled “tapErr()”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.
Type Parameters
Section titled “Type Parameters”T
E
Parameters
Section titled “Parameters”result
Section titled “result”Result<T, E>
The Result to tap
(error) => void
Side-effect function called with the error on failure
Returns
Section titled “Returns”Result<T, E>
The same Result for chaining
Example
Section titled “Example”Result.tapErr(result, error => console.error('Failed:', error.message));tapOk()
Section titled “tapOk()”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.
Type Parameters
Section titled “Type Parameters”T
E
Parameters
Section titled “Parameters”result
Section titled “result”Result<T, E>
The Result to tap
(value) => void
Side-effect function called with the value on success
Returns
Section titled “Returns”Result<T, E>
The same Result for chaining
Example
Section titled “Example”Result.tapOk(result, value => console.log('Success:', value.messageId));unwrap()
Section titled “unwrap()”unwrap<
T,E>(result):T
Extract the value or throw the error
Type Parameters
Section titled “Type Parameters”T
E
Parameters
Section titled “Parameters”result
Section titled “result”Result<T, E>
Returns
Section titled “Returns”T
unwrapOr()
Section titled “unwrapOr()”unwrapOr<
T,E>(result,defaultValue):T
Extract the value or return a default
Type Parameters
Section titled “Type Parameters”T
E
Parameters
Section titled “Parameters”result
Section titled “result”Result<T, E>
defaultValue
Section titled “defaultValue”T
Returns
Section titled “Returns”T
unwrapOrElse()
Section titled “unwrapOrElse()”unwrapOrElse<
T,E>(result,fn):T
Extract the value or compute a default from the error
Type Parameters
Section titled “Type Parameters”T
E
Parameters
Section titled “Parameters”result
Section titled “result”Result<T, E>
(error) => T
Returns
Section titled “Returns”T