Chains the next computation using the error value of a Result or ResultAsync. If the original result is a Success, it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.
The input Result or ResultAsync.
The result type returned by fn.
fn
import { Result } from '@praha/byethrow';const result = Result.pipe( Result.succeed(42), Result.orElse((error) => Result.succeed(0)),);// { type: 'Success', value: 42 } Copy
import { Result } from '@praha/byethrow';const result = Result.pipe( Result.succeed(42), Result.orElse((error) => Result.succeed(0)),);// { type: 'Success', value: 42 }
const result = Result.pipe( Result.fail('original error'), Result.orElse((error) => Result.succeed('default value')),);// result: { type: 'Success', value: 'default value' } Copy
const result = Result.pipe( Result.fail('original error'), Result.orElse((error) => Result.succeed('default value')),);// result: { type: 'Success', value: 'default value' }
const result = Result.pipe( Result.fail('original error'), Result.orElse((error) => Result.fail('new error: ' + error)),);// result: { type: 'Failure', error: 'new error: original error' } Copy
const result = Result.pipe( Result.fail('original error'), Result.orElse((error) => Result.fail('new error: ' + error)),);// result: { type: 'Failure', error: 'new error: original error' }
pipe - It is recommended to use this function with the pipe function for better readability and composability.
Chains the next computation using the error value of a Result or ResultAsync. If the original result is a Success, it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.
Type Param: R1
The input Result or ResultAsync.
Type Param: R2
The result type returned by
fn
.Example: Success Case
Example: Failure Case (function returns a Success)
Example: Failure Case (function returns a Failure)
See
pipe - It is recommended to use this function with the pipe function for better readability and composability.