Class AsyncResult<T, E>

An async-aware Result counterpart.

Can be combined with asynchronous code without having to await anything right until the moment when you're ready to extract the final Result out of it.

Can also be combined with synchronous code for convenience.

Type Parameters

  • T

  • E

Constructors

Methods

Properties

Constructors

  • Constructs an AsyncResult from a Result or a Promise of a Result.

    Type Parameters

    • T

    • E

    Parameters

    Returns AsyncResult<T, E>

    Example

    const ar = new AsyncResult(Promise.resolve('value'))
    

Methods

  • Calls mapper if the result is Ok, otherwise keeps the Err value intact. This function can be used for control flow based on Result values.

    Type Parameters

    • T2

    • E2

    Parameters

    Returns AsyncResult<T2, E | E2>

    Example

    let goodResult = Ok(1).toAsyncResult()
    let badResult = Err('boo').toAsyncResult()

    await goodResult.andThen(async (value) => Ok(value * 2)).promise // Ok(2)
    await goodResult.andThen(async (value) => Err(`${value} is bad`)).promise // Err('1 is bad')
    await badResult.andThen(async (value) => Ok(value * 2)).promise // Err('boo')
  • Maps an AsyncResult<T, E> to AsyncResult<U, E> by applying a function to a contained Ok value, leaving an Err value untouched.

    This function can be used to compose the results of two functions.

    Type Parameters

    • U

    Parameters

    Returns AsyncResult<U, E>

    Example

    let goodResult = Ok(1).toAsyncResult()
    let badResult = Err('boo').toAsyncResult()

    await goodResult.map(async (value) => value * 2).promise // Ok(2)
    await badResult.andThen(async (value) => value * 2).promise // Err('boo')

Properties

promise: Promise<Result<T, E>>

A promise that resolves to a synchronous result.

Await it to convert AsyncResult<T, E> to Result<T, E>.

thenInternal: any