Chains the next computation using the success value of a Result or ResultAsync. If the original result is a Failure, 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(3), Result.andThen((x) => Result.succeed(x * 2)),);// { type: 'Success', value: 6 } Copy
import { Result } from '@praha/byethrow';const result = Result.pipe( Result.succeed(3), Result.andThen((x) => Result.succeed(x * 2)),);// { type: 'Success', value: 6 }
const result = Result.pipe( Result.fail('error'), Result.andThen((x) => Result.succeed(x * 2)),);// result: { type: 'Failure', error: 'error' } Copy
const result = Result.pipe( Result.fail('error'), Result.andThen((x) => Result.succeed(x * 2)),);// result: { type: 'Failure', error: 'error' }
const result = Result.pipe( Result.succeed(3), Result.andThen((x) => Result.fail('error: ' + x)),);// result: { type: 'Failure', error: 'error: 3' } Copy
const result = Result.pipe( Result.succeed(3), Result.andThen((x) => Result.fail('error: ' + x)),);// result: { type: 'Failure', error: 'error: 3' }
pipe - It is recommended to use this function with the pipe function for better readability and composability.
Chains the next computation using the success value of a Result or ResultAsync. If the original result is a Failure, 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 (input is a Failure)
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.