For AI agents: the complete documentation index is available at /byethrow/llms.txt, the full documentation bundle is available at /byethrow/llms-full.txt, and this page is available as Markdown at /byethrow/api/functions/Result.andThen.md.
  • English
  • Function: andThen()

    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.

    andThen(fn)Select a case to follow its path

    Type Param

    R1

    The input Result or ResultAsync.

    Type Param

    R2

    The result type returned by fn.

    Examples

    Success Case

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<number, never>
    result
    =
    import Result
    Result
    .
    function pipe<Result.Result<3, never>, Result.Result<number, never>>(a: Result.Result<3, never>, ab: (a: Result.Result<3, never>) => Result.Result<number, never>): Result.Result<number, never> (+25 overloads)

    Applies a sequence of functions to a value, from left to right.

    This utility enables function composition in a left-to-right manner. It supports up to 26 chained functions from A through Z.

    @typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
      2,
      x => x + 1,
      x => x * 3,
      x => `Result: ${x}`
    );
    // Result: 9
    @categoryUtilities
    pipe
    (
    import Result
    Result
    .
    function succeed<3>(value: 3): Result.Result<3, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (3),
    import Result
    Result
    .
    function andThen<Result.Result<3, never>, Result.Result<number, never>>(fn: (a: 3) => Result.Result<number, never>): (result: Result.Result<3, never>) => Result.Result<number, never> (+1 overload)

    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.

    @typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // { type: 'Success', value: 6 }
    @example

    Failure Case (input is a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // result: { type: 'Failure', error: 'error' }
    @example

    Failure Case (function returns a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.fail('error: ' + x)),
    );
    // result: { type: 'Failure', error: 'error: 3' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    andThen
    ((
    x: 3
    x
    ) =>
    import Result
    Result
    .
    function succeed<number>(value: number): Result.Result<number, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (
    x: 3
    x
    * 2)),
    ); // { type: 'Success', value: 6 }

    Failure Case (input is a Failure)

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<number, "error">
    result
    =
    import Result
    Result
    .
    function pipe<Result.Result<never, "error">, Result.Result<number, "error">>(a: Result.Result<never, "error">, ab: (a: Result.Result<never, "error">) => Result.Result<number, "error">): Result.Result<number, "error"> (+25 overloads)

    Applies a sequence of functions to a value, from left to right.

    This utility enables function composition in a left-to-right manner. It supports up to 26 chained functions from A through Z.

    @typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
      2,
      x => x + 1,
      x => x * 3,
      x => `Result: ${x}`
    );
    // Result: 9
    @categoryUtilities
    pipe
    (
    import Result
    Result
    .
    function fail<"error">(error: "error"): Result.Result<never, "error"> (+1 overload)

    Creates a Failure result from a given error.

    Passing a Promise is allowed, but it is recommended to await it before passing to fail.

    @typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('Something went wrong');
    // Result.Result<never, string>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail();
    // Result.Result<never, void>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    fail
    ('error'),
    import Result
    Result
    .
    function andThen<Result.Result<never, "error">, Result.Result<number, never>>(fn: (a: never) => Result.Result<number, never>): (result: Result.Result<never, "error">) => Result.Result<number, "error"> (+1 overload)

    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.

    @typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // { type: 'Success', value: 6 }
    @example

    Failure Case (input is a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // result: { type: 'Failure', error: 'error' }
    @example

    Failure Case (function returns a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.fail('error: ' + x)),
    );
    // result: { type: 'Failure', error: 'error: 3' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    andThen
    ((
    x: never
    x
    ) =>
    import Result
    Result
    .
    function succeed<number>(value: number): Result.Result<number, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (
    x: never
    x
    * 2)),
    ); // result: { type: 'Failure', error: 'error' }

    Failure Case (function returns a Failure)

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<never, string>
    result
    =
    import Result
    Result
    .
    function pipe<Result.Result<3, never>, Result.Result<never, string>>(a: Result.Result<3, never>, ab: (a: Result.Result<3, never>) => Result.Result<never, string>): Result.Result<never, string> (+25 overloads)

    Applies a sequence of functions to a value, from left to right.

    This utility enables function composition in a left-to-right manner. It supports up to 26 chained functions from A through Z.

    @typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
      2,
      x => x + 1,
      x => x * 3,
      x => `Result: ${x}`
    );
    // Result: 9
    @categoryUtilities
    pipe
    (
    import Result
    Result
    .
    function succeed<3>(value: 3): Result.Result<3, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (3),
    import Result
    Result
    .
    function andThen<Result.Result<3, never>, Result.Result<never, string>>(fn: (a: 3) => Result.Result<never, string>): (result: Result.Result<3, never>) => Result.Result<never, string> (+1 overload)

    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.

    @typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // { type: 'Success', value: 6 }
    @example

    Failure Case (input is a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // result: { type: 'Failure', error: 'error' }
    @example

    Failure Case (function returns a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.fail('error: ' + x)),
    );
    // result: { type: 'Failure', error: 'error: 3' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    andThen
    ((
    x: 3
    x
    ) =>
    import Result
    Result
    .
    function fail<string>(error: string): Result.Result<never, string> (+1 overload)

    Creates a Failure result from a given error.

    Passing a Promise is allowed, but it is recommended to await it before passing to fail.

    @typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('Something went wrong');
    // Result.Result<never, string>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail();
    // Result.Result<never, void>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    fail
    ('error: ' +
    x: 3
    x
    )),
    ); // result: { type: 'Failure', error: 'error: 3' }

    See

    pipe - It is recommended to use this function with the pipe function for better readability and composability.

    Call Signature

    andThen<R1, R2>(fn): (result) => ResultFor<R1 | R2, InferSuccess<R2>, InferFailure<R1> | InferFailure<R2>>

    Type Parameters

    R1

    R1 extends ResultMaybeAsync<any, any>

    R2

    R2 extends ResultMaybeAsync<any, any>

    Parameters

    fn

    (a) => R2

    Returns

    (result) => ResultFor<R1 | R2, InferSuccess<R2>, InferFailure<R1> | InferFailure<R2>>

    Call Signature

    andThen<F>(fn): <R1>(result) => ResultFor<R1 | ReturnType<F>, InferSuccess<F>, InferFailure<R1> | InferFailure<F>>

    Type Parameters

    F

    F extends (a) => ResultMaybeAsync<any, any>

    Parameters

    fn

    F

    Returns

    <R1>(result) => ResultFor<R1 | ReturnType<F>, InferSuccess<F>, InferFailure<R1> | InferFailure<F>>