Interface Result<V, E>

A Result jonad, a subtype of Either.

Represents a value ("Ok") or an error ("Err"). Associates a set of utility functions to work with the jonad.

interface Result<V, E> {
    andThen<T>(mapper: Mapper<V, Result<T, E>>): Result<T, E>;
    andThenAsync<T>(mapper: AsyncMapper<V, Result<T, E>>): Promise<Result<T, E>>;
    asNullable(): Result<Option<V>, E>;
    getLeftOrThrow(): V;
    getRightOrThrow(): E;
    isErr(): boolean;
    isLeft(): boolean;
    isOk(): boolean;
    isRight(): boolean;
    leftOr(fallback: V | Mapper<E, V>): V;
    leftOrAsync(fallback: V | Promise<V> | AsyncMapper<E, V>): Promise<V>;
    map<T>(mapper: Mapper<V, T>): Result<T, E>;
    mapAsync<T>(mapper: AsyncMapper<V, T>): Promise<Result<T, E>>;
    mapErr<T>(mapper: Mapper<E, T>): Result<V, T>;
    mapErrAsync<T>(mapper: AsyncMapper<E, T>): Promise<Result<V, T>>;
    mapLeft<V>(mapper: Mapper<V, V>): Either<V, E>;
    mapLeftAsync<V>(mapper: AsyncMapper<V, V>): Promise<Either<V, E>>;
    mapRight<V>(mapper: Mapper<E, V>): Either<V, V>;
    mapRightAsync<V>(mapper: AsyncMapper<E, V>): Promise<Either<V, V>>;
    match<V>(onLeft: Mapper<V, V>, onRight: Mapper<E, V>): V;
    matchAsync<V>(onLeft: AsyncMapper<V, V>, onRight: AsyncMapper<E, V>): Promise<V>;
    rightOr(fallback: E | Mapper<V, E>): E;
    rightOrAsync(fallback: E | Promise<E> | AsyncMapper<V, E>): Promise<E>;
    someOrNone(): Option<V>;
    tapLeft(callback: Consumer<V>): Either<V, E>;
    tapLeftAsync(callback: AsyncConsumer<V>): Promise<Either<V, E>>;
    tapRight(callback: Consumer<E>): Either<V, E>;
    tapRightAsync(callback: AsyncConsumer<E>): Promise<Either<V, E>>;
    valueOr(fallback: V | Mapper<E, V>): V;
    valueOrAsync(fallback: V | Promise<V> | AsyncMapper<E, V>): Promise<V>;
}

Type Parameters

  • V
  • E extends Error

Hierarchy (view full)

Methods

  • Chains a new Result to the current one if it is Ok.

    Type Parameters

    • T

    Parameters

    • mapper: Mapper<V, Result<T, E>>

      The mapper function to apply to the value if it is an Ok.

    Returns Result<T, E>

    A new Result with the mapped value if it is an Ok, otherwise the value as-is.

  • Chains a new Result to the current one if it is Ok, but asynchronously.

    Type Parameters

    • T

    Parameters

    • mapper: AsyncMapper<V, Result<T, E>>

      The async mapper function to apply to the value if it is an Ok.

    Returns Promise<Result<T, E>>

    A new Result with the mapped value if it is an Ok, otherwise the value as-is.

  • Maps the Result so the Ok value is wrapped in an Option based on its presence.

    If the Result is an Ok and the value is not null or undefined, then the inner value will become Some. If the Result is an Ok and the value is null or undefined, then the inner value will become None. If the Result is an Err, then nothing will happen.

    Returns Result<Option<V>, E>

    A new Result with the Ok value wrapped in an Option based on its presence.

  • Returns the value if it is a Left, otherwise throws an error.

    NOTE: This function is unsafe and should only be used for testing.

    Returns V

    The value if it is a Left.

    GetValueError if the value is a Right.

  • Returns the value if it is a Right, otherwise throws an error.

    NOTE: This function is unsafe and should only be used for testing.

    Returns E

    The value if it is a Right.

    GetValueError if the value is a Left.

  • Checks if the value is an Err.

    Returns boolean

    true if the value is an Err, false otherwise.

    const okish = Result.ok(123);
    okish.isErr(); // false

    const errish = Resulr.error("An error");
    errish.isErr(); // true
  • Checks if the value is a Left.

    Returns boolean

    true if the value is a Left, false otherwise.

    const value = Either.left(42);
    value.isLeft(); // => true
  • Checks if the value is an Ok.

    Returns boolean

    true if the value is an Ok, false otherwise.

    const okish = Result.ok(123);
    result.isOk(); // true

    const errish = Resulr.error("An error");
    result.isOk(); // false
  • Checks if the value is a Right.

    Returns boolean

    true if the value is a Right, false otherwise.

    const value = Either.right(42);
    value.isRight(); // => true
  • Returns the value if it is a Left, otherwise returns a default value.

    Parameters

    • fallback: V | Mapper<E, V>

      The default value (as-is or produced from a callback) to return if the value is a Right.

    Returns V

    The value if it is a Left, otherwise the default value.

    const value = Either.left(42);
    value.leftOr(0); // => 42
    const value = Either.right(42);
    value.leftOr(0); // => 0
  • Returns the value if it is a Left, otherwise returns a default value asynchronously.

    Parameters

    • fallback: V | Promise<V> | AsyncMapper<E, V>

      The default value (as-is or produced from a callback) to return if the value is a Right.

    Returns Promise<V>

    The value if it is a Left, otherwise the default value.

    const value = Either.left(42);
    await value.leftOrAsync(async () => Promise.resolve(0)); // => 42
    const value = Either.right(42);
    await value.leftOrAsync(async () => Promise.resolve(0)); // => 0
  • Maps the value if it is an Ok.

    Type Parameters

    • T

    Parameters

    • mapper: Mapper<V, T>

      The mapper function to apply to the value if it is an Ok.

    Returns Result<T, E>

    A new Result with the mapped value if it is an Ok, otherwise the result as-is.

    const okish = Result.ok(123);
    okish.map(value => value * 2); // Ok(246)

    const errish = Resulr.error("An error");
    errish.map(value => value * 2); // Err("An error")
  • Maps the value if it is an Ok, but asynchronously.

    Type Parameters

    • T

    Parameters

    • mapper: AsyncMapper<V, T>

      The async mapper function to apply to the value if it is an Ok.

    Returns Promise<Result<T, E>>

    A new Result with the mapped value if it is an Ok, otherwise the result as-is.

    const okish = Result.ok(123);
    okish.mapAsync(async value => value * 2); // Promise(246)

    const errish = Resulr.error("An error");
    errish.mapAsync(async value => value * 2);
  • Maps the error if it is an Err, otherwise returns the error as-is.

    Type Parameters

    • T extends Error

    Parameters

    • mapper: Mapper<E, T>

      The mapper function to apply to the error if it is an Err.

    Returns Result<V, T>

    A new Result with the mapped error if it is an Err, otherwise the result as-is.

    const okish = Result.ok(123);
    okish.mapErr(error => new Error("An error")); // Ok(123)

    const errish = Resulr.error("An error");
    errish.mapErr(error => new Error("Another error"));
  • Maps the error if it is an Err, but asynchronously.

    Type Parameters

    • T extends Error

    Parameters

    • mapper: AsyncMapper<E, T>

      The async mapper function to apply to the error if it is an Err.

    Returns Promise<Result<V, T>>

    A new Result with the mapped error if it is an Err, otherwise the result as-is.

    const okish = Result.ok(123);
    okish.mapErrAsync(async error => new Error("An error")); // Ok(123)

    const errish = Resulr.error("An error");
    errish.mapErrAsync(async error => new Error("Another error")); // Promise(Err("Another error"))
  • Maps the value if it is a Left, otherwise returns the value as-is.

    Type Parameters

    • V

    Parameters

    • mapper: Mapper<V, V>

      The function to apply to the value if it is a Left.

    Returns Either<V, E>

    A new Either with the mapped value if it is a Left, otherwise the value as-is.

    const value = Either.left(1);
    value.mapLeft((value) => value + 1); // => Left(2)
    const value = Either.right(1);
    value.mapLeft((value) => value + 1); // => Right(1)
  • Maps the value if it is a Left asynchronously, otherwise returns the value as-is.

    Type Parameters

    • V

    Parameters

    • mapper: AsyncMapper<V, V>

      The async function to apply to the value if it is a Left.

    Returns Promise<Either<V, E>>

    A new Either with the mapped value if it is a Left, otherwise the value as-is.

    const value = Either.left(1);
    await value.mapLeftAsync(async (value) => Promise.resolve(value + 1)); // => Left(2)
    const value = Either.right(1);
    await value.mapLeftAsync(async (value) => Promise.resolve(value + 1)); // => Right(1)
  • Maps the value if it is a Right, otherwise returns the value as-is.

    Type Parameters

    • V

    Parameters

    • mapper: Mapper<E, V>

      The function to apply to the value if it is a Right.

    Returns Either<V, V>

    A new Either with the mapped value if it is a Right, otherwise the value as-is.

    const value = Either.right(1);
    value.mapRight((value) => value + 1); // => Right(2)
    const value = Either.left(1);
    value.mapRight((value) => value + 1); // => Left(1)
  • Maps the value if it is a Right asynchronously, otherwise returns the value as-is.

    Type Parameters

    • V

    Parameters

    • mapper: AsyncMapper<E, V>

      The async function to apply to the value if it is a Right.

    Returns Promise<Either<V, V>>

    A new Either with the mapped value if it is a Right, otherwise the value as-is.

    const value = Either.right(1);
    await value.mapRightAsync(async (value) => Promise.resolve(value + 1)); // => Right(2)
    const value = Either.left(1);
    await value.mapRightAsync(async (value) => Promise.resolve(value + 1)); // => Left(1)
  • Matches the jonad by calling the appropriate callback based on the value type.

    Type Parameters

    • V

    Parameters

    • onLeft: Mapper<V, V>

      If the value is a Left, this callback is called with the value.

    • onRight: Mapper<E, V>

      If the value is a Right, this callback is called with the value.

    Returns V

    The result of the callback that was called.

    const value = Either.left(42);
    value.match(
    (lvalue) => console.log(`Answer to life: ${lvalue}`),
    (rvalue) => console.log(`Hello, ${rvalue}`)
    ); // => "Answer to life: 42"
    const value = Either.right("world!");
    value.match(
    (value) => console.log(`Answer to life: ${lvalue}`),
    (value) => console.log(`Hello, ${rvalue}`)
    ); // => "Hello, world!"
  • Matches the jonad by calling the appropriate callback based on the value type asynchronously.

    Type Parameters

    • V

    Parameters

    • onLeft: AsyncMapper<V, V>

      If the value is a Left, this callback is called with the value.

    • onRight: AsyncMapper<E, V>

      If the value is a Right, this callback is called with the value.

    Returns Promise<V>

    The result of the callback that was called.

    const value = Either.left(1);
    await value.matchAsync(
    async (lvalue) => `${lvalue} + 2 = ${lvalue + 2}`,
    async (rvalue) => `1 + ${rvalue} = ${1 + rvalue}`
    ); // => "1 + 2 = 3"
    const value = Either.right(2);
    await value.matchAsync(
    async (lvalue) => `${lvalue} + 2 = ${lvalue + 2}`,
    async (rvalue) => `1 + ${rvalue} = ${1 + rvalue}`
    ); // => "1 + 2 = 3"
  • Returns the value if it is a Right, otherwise returns a default value.

    Parameters

    • fallback: E | Mapper<V, E>

      The default value (as-is or produced from a callback) to return if the value is a Left.

    Returns E

    The value if it is a Right, otherwise the default value.

    const value = Either.right(42);
    value.rightOr(0); // => 42
    const value = Either.left(42);
    value.rightOr(0); // => 0
  • Returns the value if it is a Right, otherwise returns a default value asynchronously.

    Parameters

    • fallback: E | Promise<E> | AsyncMapper<V, E>

      The default value (as-is or produced from a callback) to return if the value is a Left.

    Returns Promise<E>

    The value if it is a Right, otherwise the default value.

    const value = Either.right(42);
    await value.rightOrAsync(async () => Promise.resolve(0)); // => 42
    const value = Either.left(42);
    await value.rightOrAsync(async () => Promise.resolve(0)); // => 0
  • Maps the Result into an Option.

    If the Result is an Ok and the value is not null or undefined, then it will be Some with that value. If the Result is an Ok and the value is null or undefined, then it will be None. If the Result is an Err, then it will be None with the error discarded.

    Returns Option<V>

    Some if the value is Ok and present, otherwise None.

  • Applies a function to the value if it is a Left, returning itself.

    Parameters

    • callback: Consumer<V>

      The function to apply to the value if it is a Left.

    Returns Either<V, E>

    The Either as-is.

    const value = Either.left([1, 2]);
    value.tapLeft((value) => value.push(3)); // => Left([1, 2, 3])
    const value = Either.right([1, 2]);
    value.tapLeft((value) => value.push(3)); // => Right([1, 2])
  • Asynchronously applies a function to the value if it is a Left, returning itself.

    Parameters

    • callback: AsyncConsumer<V>

      The async function to apply to the value if it is a Left.

    Returns Promise<Either<V, E>>

    The Either as-is.

    const value = Either.left([1, 2]);
    await value.tapLeftAsync(async (value) => value.push(3)); // => Left([1, 2, 3])
    const value = Either.right([1, 2]);
    await value.tapLeftAsync(async (value) => value.push(3)); // => Right([1, 2])
  • Applies a function to the value if it is a Right, returning itself.

    Parameters

    • callback: Consumer<E>

      The function to apply to the value if it is a Right.

    Returns Either<V, E>

    The Either as-is.

    const value = Either.right([1, 2]);
    value.tapRight((value) => value.push(3)); // => Right([1, 2, 3])
    const value = Either.left([1, 2]);
    value.tapRight((value) => value.push(3)); // => Left([1, 2])
  • Asynchronously applies a function to the value if it is a Right, returning itself.

    Parameters

    • callback: AsyncConsumer<E>

      The async function to apply to the value if it is a Right.

    Returns Promise<Either<V, E>>

    The Either as-is.

    const value = Either.right([1, 2]);
    await value.tapRightAsync(async (value) => value.push(3)); // => Right([1, 2, 3])
    const value = Either.left([1, 2]);
    await value.tapRightAsync(async (value) => value.push(3)); // => Left([1, 2])
  • Returns the value if it is an Ok, otherwise returns a default value.

    Parameters

    • fallback: V | Mapper<E, V>

      The default value (as-is or produced from a callback) to return if the value is an Err.

    Returns V

    The value if it is an Ok, otherwise the default value.

    const okish = Result.ok(123);
    okish.valueOr(0); // 123

    const errish = Resulr.error("An error");
    errish.valueOr(0);
  • Returns the value if it is an Ok, otherwise returns a default value asynchronously.

    Parameters

    • fallback: V | Promise<V> | AsyncMapper<E, V>

      The default value (as-is or produced from a callback) to return if the value is an Err.

    Returns Promise<V>

    The value if it is an Ok, otherwise the default value.

    const okish = Result.ok(123);
    okish.valueOrAsync(async () => 0); // Promise(123)

    const errish = Resulr.error("An error");
    errish.valueOrAsync(Promise.resolve(0)); // Promise(0)