• English
  • Creating Results

    Now that you understand the Result type, let's learn how to create them using the library's helper functions.

    Creating Success Results with succeed

    The succeed function creates a Success result:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    // With a value const
    const success: Result.Result<42, never>
    success
    =
    import Result
    Result
    .
    const succeed: <42>(value: 42) => Result.Result<42, never> (+1 overload)
    succeed
    (42);
    // Type: Result.Result<number, never> // Without a value (void) const
    const voidSuccess: Result.Result<void, never>
    voidSuccess
    =
    import Result
    Result
    .
    const succeed: () => Result.ResultFor<never, void, never> (+1 overload)
    succeed
    ();
    // Type: Result.Result<void, never>

    With Async Values

    If you pass a Promise, succeed automatically returns a ResultAsync:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const asyncSuccess: Result.ResultAsync<string, never>
    asyncSuccess
    =
    import Result
    Result
    .
    const succeed: <Promise<string>>(value: Promise<string>) => Result.ResultAsync<string, never> (+1 overload)
    succeed
    (
    var Promise: PromiseConstructor

    Represents the completion of an asynchronous operation

    Promise
    .
    PromiseConstructor.resolve<string>(value: string): Promise<string> (+2 overloads)

    Creates a new resolved promise for the provided value.

    @paramvalue A promise.@returnsA promise whose internal state matches the provided promise.
    resolve
    ('hello'));
    // Type: Result.ResultAsync<string, never> const
    const resolved: Result.Result<string, never>
    resolved
    = await
    const asyncSuccess: Result.ResultAsync<string, never>
    asyncSuccess
    ;
    // Type: Result.Result<string, never>

    Creating Failure Results with fail

    The fail function creates a Failure result:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    // With an error value const
    const failure: Result.Result<never, "Something went wrong">
    failure
    =
    import Result
    Result
    .
    const fail: <"Something went wrong">(error: "Something went wrong") => Result.Result<never, "Something went wrong"> (+1 overload)
    fail
    ('Something went wrong');
    // Type: Result.Result<never, string> // Without a value (void) const
    const voidFailure: Result.Result<never, void>
    voidFailure
    =
    import Result
    Result
    .
    const fail: () => Result.ResultFor<never, never, void> (+1 overload)
    fail
    ();
    // Type: Result.Result<never, void>

    With Async Errors

    Like succeed, if you pass a Promise, fail returns a ResultAsync:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const asyncFailure: Result.ResultAsync<never, string>
    asyncFailure
    =
    import Result
    Result
    .
    const fail: <Promise<string>>(error: Promise<string>) => Result.ResultAsync<never, string> (+1 overload)
    fail
    (
    var Promise: PromiseConstructor

    Represents the completion of an asynchronous operation

    Promise
    .
    PromiseConstructor.resolve<string>(value: string): Promise<string> (+2 overloads)

    Creates a new resolved promise for the provided value.

    @paramvalue A promise.@returnsA promise whose internal state matches the provided promise.
    resolve
    ('async error'));
    // Type: Result.ResultAsync<never, string> const
    const resolved: Result.Result<never, string>
    resolved
    = await
    const asyncFailure: Result.ResultAsync<never, string>
    asyncFailure
    ;
    // Type: Result.Result<never, string>

    Starting a Pipeline with do

    The do function is a convenient way to start a pipeline with an empty object:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const start: Result.Result<{}, never>
    start
    =
    import Result
    Result
    .
    function do(): Result.Result<{}, never>
    export do

    Alias for succeed({}). Commonly used as a neutral base value in functional chains or monadic pipelines.

    @function@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.do();
    // Result.Result<{}, never>
    @categoryCreators
    do
    ();
    // Type: Result.Result<{}, never>

    This is especially useful when building up an object step by step (we'll cover this pattern with bind in a later section):

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<{
        name: "Alice";
        age: 30;
    }, never>
    result
    =
    import Result
    Result
    .
    const pipe: <Result.Result<{}, never>, Result.Result<{
        name: "Alice";
    }, never>, Result.Result<{
        name: "Alice";
        age: 30;
    }, never>>(a: Result.Result<{}, never>, ab: (a: Result.Result<{}, never>) => Result.Result<{
        name: "Alice";
    }, never>, bc: (b: Result.Result<{
        name: "Alice";
    }, never>) => Result.Result<{
        name: "Alice";
        age: 30;
    }, never>) => Result.Result<{
        name: "Alice";
        age: 30;
    }, never> (+25 overloads)
    pipe
    (
    import Result
    Result
    .
    function do(): Result.Result<{}, never>
    export do

    Alias for succeed({}). Commonly used as a neutral base value in functional chains or monadic pipelines.

    @function@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.do();
    // Result.Result<{}, never>
    @categoryCreators
    do
    (),
    import Result
    Result
    .
    const bind: <"name", Result.Result<{}, never>, Result.Result<"Alice", never>>(name: "name", fn: (a: {}) => Result.Result<"Alice", never>) => (result: Result.Result<{}, never>) => Result.Result<{
        name: "Alice";
    }, never> (+1 overload)
    bind
    ('name', () =>
    import Result
    Result
    .
    const succeed: <"Alice">(value: "Alice") => Result.Result<"Alice", never> (+1 overload)
    succeed
    ('Alice')),
    import Result
    Result
    .
    const bind: <"age", Result.Result<{
        name: "Alice";
    }, never>, Result.Result<30, never>>(name: "age", fn: (a: {
        name: "Alice";
    }) => Result.Result<30, never>) => (result: Result.Result<{
        name: "Alice";
    }, never>) => Result.Result<{
        name: "Alice";
        age: 30;
    }, never> (+1 overload)
    bind
    ('age', () =>
    import Result
    Result
    .
    const succeed: <30>(value: 30) => Result.Result<30, never> (+1 overload)
    succeed
    (30)),
    ); // Type: Result.Result<{ name: string; age: number }, never>

    References

    FunctionPurpose
    succeed(value)Create a success result
    fail(error)Create a failure result
    do()Start with empty object