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/guide/tutorial/resolving/unwrapping.md.
  • English
  • Unwrapping Results

    At some point, you need to take the value out of a Result and hand it to code that doesn't know about Results, such as a web framework or a CLI runner. unwrap and unwrapError extract the contents of a Result as a plain value.

    Extracting the Value with unwrap

    unwrap(result, defaultValue?)Select a case to follow its path

    unwrap returns the success value of a Result:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const value: 42
    value
    =
    import Result
    Result
    .
    function unwrap<Result.Result<42, never>>(result: Result.Result<42, never>): 42 (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (
    import Result
    Result
    .
    function succeed<42>(value: 42): Result.Result<42, 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
    (42));
    // value: 42

    If the Result is a Failure, unwrap throws its error:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const value: never
    value
    =
    import Result
    Result
    .
    function unwrap<Result.Result<never, Error>>(result: Result.Result<never, Error>): never (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (
    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
    (new
    var Error: ErrorConstructor
    new (message?: string, options?: ErrorOptions) => Error (+1 overload)
    Error
    ('Something went wrong')));
    // Throws: Error('Something went wrong')

    Providing a Default Value

    Pass a default value as the second argument to return it instead of throwing:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const fallback: 0
    fallback
    =
    import Result
    Result
    .
    function unwrap<Result.Result<never, "error">, 0>(result: Result.Result<never, "error">, defaultValue: 0): 0 (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (
    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'), 0);
    // fallback: 0 const
    const value: 0 | 42
    value
    =
    import Result
    Result
    .
    function unwrap<Result.Result<42, never>, 0>(result: Result.Result<42, never>, defaultValue: 0): 0 | 42 (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (
    import Result
    Result
    .
    function succeed<42>(value: 42): Result.Result<42, 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
    (42), 0);
    // value: 42 (the default is ignored)

    Extracting the Error with unwrapError

    unwrapError(result, defaultValue?)Select a case to follow its path

    unwrapError is the counterpart of unwrap. It returns the error value of a Result:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const error: "Something went wrong"
    error
    =
    import Result
    Result
    .
    function unwrapError<Result.Result<never, "Something went wrong">>(result: Result.Result<never, "Something went wrong">): "Something went wrong" (+3 overloads)

    Extracts the error value from a Result or ResultAsync .

    If the input is a Success , it will throw the success value or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Failure Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.fail('Oops');
    const error = Result.unwrapError(result); // 'Oops'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.fail('Oops');
    const error = Result.unwrapError(result, 'default'); // 'Oops'
    @example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.succeed(100);
    const error = Result.unwrapError(result); // throws 100
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.succeed(100);
    const error = Result.unwrapError(result, 0); // 0
    @seeassertFailure - When used with assertFailure , you can safely unwrap the Result .@categoryUnwraps
    unwrapError
    (
    import Result
    Result
    .
    function fail<"Something went wrong">(error: "Something went wrong"): Result.Result<never, "Something went wrong"> (+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
    ('Something went wrong'));
    // error: 'Something went wrong'

    If the Result is a Success, unwrapError throws its value, unless a default value is provided:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const error: never
    error
    =
    import Result
    Result
    .
    function unwrapError<Result.Result<42, never>>(result: Result.Result<42, never>): never (+3 overloads)

    Extracts the error value from a Result or ResultAsync .

    If the input is a Success , it will throw the success value or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Failure Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.fail('Oops');
    const error = Result.unwrapError(result); // 'Oops'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.fail('Oops');
    const error = Result.unwrapError(result, 'default'); // 'Oops'
    @example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.succeed(100);
    const error = Result.unwrapError(result); // throws 100
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.succeed(100);
    const error = Result.unwrapError(result, 0); // 0
    @seeassertFailure - When used with assertFailure , you can safely unwrap the Result .@categoryUnwraps
    unwrapError
    (
    import Result
    Result
    .
    function succeed<42>(value: 42): Result.Result<42, 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
    (42));
    // Throws: 42 const
    const fallback: "No error"
    fallback
    =
    import Result
    Result
    .
    function unwrapError<Result.Result<42, never>, "No error">(result: Result.Result<42, never>, defaultValue: "No error"): "No error" (+3 overloads)

    Extracts the error value from a Result or ResultAsync .

    If the input is a Success , it will throw the success value or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Failure Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.fail('Oops');
    const error = Result.unwrapError(result); // 'Oops'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.fail('Oops');
    const error = Result.unwrapError(result, 'default'); // 'Oops'
    @example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.succeed(100);
    const error = Result.unwrapError(result); // throws 100
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = Result.succeed(100);
    const error = Result.unwrapError(result, 0); // 0
    @seeassertFailure - When used with assertFailure , you can safely unwrap the Result .@categoryUnwraps
    unwrapError
    (
    import Result
    Result
    .
    function succeed<42>(value: 42): Result.Result<42, 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
    (42), 'No error');
    // fallback: 'No error'

    Unwrapping in a Pipeline

    When called without a Result, both functions return a function that unwraps its argument. This lets you use them as the last step of a pipeline:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const value: number
    value
    =
    import Result
    Result
    .
    function pipe<Result.Result<21, never>, Result.Result<number, never>, number>(a: Result.Result<21, never>, ab: (a: Result.Result<21, never>) => Result.Result<number, never>, bc: (b: Result.Result<number, never>) => number): number (+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<21>(value: 21): Result.Result<21, 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
    (21),
    import Result
    Result
    .
    function map<Result.Result<21, never>, number>(fn: (a: 21) => number): (result: Result.Result<21, never>) => Result.Result<number, never> (+1 overload)

    Applies a transformation function to the success value of a Result or ResultAsync . If the input is a Failure , it will be returned unchanged.

    @typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The transformed success value type.@example

    Success Case

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

    Failure Case (unchanged)

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

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (), // or Result.unwrap(defaultValue)
    ); // value: 42

    Async Results

    Both functions accept a ResultAsync and return a Promise of the plain value:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const value: 42
    value
    = await
    import Result
    Result
    .
    function unwrap<Promise<Result.Result<42, never>>>(result: Promise<Result.Result<42, never>>): Promise<42> (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (
    var Promise: PromiseConstructor

    Represents the completion of an asynchronous operation

    Promise
    .
    PromiseConstructor.resolve<Result.Result<42, never>>(value: Result.Result<42, never>): Promise<Result.Result<42, never>> (+2 overloads)

    Creates a new resolved promise for the provided value.

    @paramvalue A promise.@returnsA promise whose internal state matches the provided promise.
    resolve
    (
    import Result
    Result
    .
    function succeed<42>(value: 42): Result.Result<42, 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
    (42)));
    // value: 42 const
    const fallback: 0
    fallback
    = await
    import Result
    Result
    .
    function unwrap<Promise<Result.Result<never, "error">>, 0>(result: Promise<Result.Result<never, "error">>, defaultValue: 0): Promise<0> (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (
    var Promise: PromiseConstructor

    Represents the completion of an asynchronous operation

    Promise
    .
    PromiseConstructor.resolve<Result.Result<never, "error">>(value: Result.Result<never, "error">): Promise<Result.Result<never, "error">> (+2 overloads)

    Creates a new resolved promise for the provided value.

    @paramvalue A promise.@returnsA promise whose internal state matches the provided promise.
    resolve
    (
    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')), 0);
    // fallback: 0

    Where to Unwrap

    At the Boundaries of Your Application

    Unwrap at the boundaries of your application, such as HTTP handlers and CLI entry points, where plain values have to be handed to the outside world. Handle the errors you expect first, and let unwrap throw only what remains:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const handleRequest: (req: Request) => Promise<Response>
    handleRequest
    = async (
    req: Request
    req
    : Request):
    interface Promise<T>

    Represents the completion of an asynchronous operation

    Promise
    <Response> => {
    return await
    import Result
    Result
    .
    function pipe<Result.ResultAsync<number, "NotFound" | "Unexpected">, Result.ResultAsync<Response, "NotFound" | "Unexpected">, Result.ResultAsync<Response, "Unexpected">, Promise<Response>>(a: Result.ResultAsync<number, "NotFound" | "Unexpected">, ab: (a: Result.ResultAsync<number, "NotFound" | "Unexpected">) => Result.ResultAsync<Response, "NotFound" | "Unexpected">, bc: (b: Result.ResultAsync<Response, "NotFound" | "Unexpected">) => Result.ResultAsync<...>, cd: (c: Result.ResultAsync<...>) => Promise<...>): Promise<...> (+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
    (
    const processRequest: (req: Request) => Result.ResultAsync<number, "NotFound" | "Unexpected">
    processRequest
    (
    req: Request
    req
    ),
    import Result
    Result
    .
    function map<Result.ResultAsync<number, "NotFound" | "Unexpected">, Response>(fn: (a: number) => Response): (result: Result.ResultAsync<number, "NotFound" | "Unexpected">) => Result.ResultAsync<Response, "NotFound" | "Unexpected"> (+1 overload)

    Applies a transformation function to the success value of a Result or ResultAsync . If the input is a Failure , it will be returned unchanged.

    @typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The transformed success value type.@example

    Success Case

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

    Failure Case (unchanged)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.map((x) => x * 10),
    );
    // { type: 'Failure', error: 'error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    map
    ((
    result: number
    result
    ) => {
    return new
    var Response: new (body?: BodyInit | null, init?: ResponseInit) => Response

    The Response interface of the Fetch API represents the response to a request.

    MDN Reference

    Response
    (
    var JSON: JSON

    An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.

    JSON
    .
    JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)

    Converts a JavaScript value to a JavaScript Object Notation (JSON) string.

    @paramvalue A JavaScript value, usually an object or array, to be converted.@paramreplacer A function that transforms the results.@paramspace Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.@throws{TypeError} If a circular reference or a BigInt value is found.
    stringify
    (
    result: number
    result
    ), {
    ResponseInit.status?: number | undefined
    status
    : 200 });
    }),
    import Result
    Result
    .
    function orElse<Result.ResultAsync<Response, "NotFound" | "Unexpected">, Result.Success<Response> | Result.Failure<"Unexpected">>(fn: (a: "NotFound" | "Unexpected") => Result.Success<Response> | Result.Failure<"Unexpected">): (result: Result.ResultAsync<Response, "NotFound" | "Unexpected">) => Result.ResultAsync<Response, "Unexpected"> (+1 overload)

    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.

    @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(42),
    Result.orElse((error) => Result.succeed(0)),
    );
    // { type: 'Success', value: 42 }
    @example

    Failure Case (function returns a Success)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('original error'),
    Result.orElse((error) => Result.succeed('default value')),
    );
    // result: { type: 'Success', value: 'default value' }
    @example

    Failure Case (function returns a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('original error'),
    Result.orElse((error) => Result.fail('new error: ' + error)),
    );
    // result: { type: 'Failure', error: 'new error: original error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    orElse
    ((
    error: "NotFound" | "Unexpected"
    error
    ) => {
    switch (
    error: "NotFound" | "Unexpected"
    error
    ) {
    case 'NotFound': return
    import Result
    Result
    .
    function succeed<Response>(value: Response): Result.Result<Response, 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
    (new
    var Response: new (body?: BodyInit | null, init?: ResponseInit) => Response

    The Response interface of the Fetch API represents the response to a request.

    MDN Reference

    Response
    ('Not Found', {
    ResponseInit.status?: number | undefined
    status
    : 404 }));
    default: return
    import Result
    Result
    .
    function fail<"Unexpected">(error: "Unexpected"): Result.Result<never, "Unexpected"> (+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: "Unexpected"
    error
    );
    } }), // Remaining errors are thrown here, where infrastructure-level // error monitoring such as Sentry or CloudWatch can detect them.
    import Result
    Result
    .
    function unwrap<Result.ResultAsync<Response, "Unexpected">>(): (result: Result.ResultAsync<Response, "Unexpected">) => Promise<Response> (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (),
    ); };

    To make sure at compile time that unwrap never throws, combine it with assertSuccess on the next page.

    Not in the Middle of Your Logic

    Avoid unwrapping in the middle of your logic. Doing so leaves the Result world too early and turns expected failures back into exceptions. Keep values wrapped and transform them with functions such as map, andThen, and orElse instead:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    // ❌ Don't do this const
    const bad: number
    bad
    =
    import Result
    Result
    .
    function unwrap<Result.Result<42, never>>(result: Result.Result<42, never>): 42 (+3 overloads)

    Extracts the success value from a Result or ResultAsync .

    If the input is a Failure , it will throw the error or return the default value if provided.

    @typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    const value = Result.unwrap(result, 0); // 0
    @seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps
    unwrap
    (
    import Result
    Result
    .
    function succeed<42>(value: 42): Result.Result<42, 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
    (42)) * 2;
    // ✅ Do this instead const
    const good: Result.Result<number, never>
    good
    =
    import Result
    Result
    .
    function pipe<Result.Result<42, never>, Result.Result<number, never>>(a: Result.Result<42, never>, ab: (a: Result.Result<42, 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<42>(value: 42): Result.Result<42, 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
    (42),
    import Result
    Result
    .
    function map<Result.Result<42, never>, number>(fn: (a: 42) => number): (result: Result.Result<42, never>) => Result.Result<number, never> (+1 overload)

    Applies a transformation function to the success value of a Result or ResultAsync . If the input is a Failure , it will be returned unchanged.

    @typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The transformed success value type.@example

    Success Case

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

    Failure Case (unchanged)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.map((x) => x * 10),
    );
    // { type: 'Failure', error: 'error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    map
    ((
    x: 42
    x
    ) =>
    x: 42
    x
    * 2),
    );

    References

    APIPurpose
    unwrap(result)Extract the success value, or throw / return a default on failure
    unwrapError(result)Extract the error value, or throw / return a default on success