@lppedd/mini-result - v0.5.0
    Preparing search index...

    Interface AsyncResult<V, E>

    Allows performing asynchronous operations on a Result.

    interface AsyncResult<V, E> {
        catchAsync<RV = V>(
            fn: (e: E) => Ok<RV, E> | Promise<Ok<RV, E>>,
        ): AsyncResult<V | RV, E>;
        catchAsync<RE = E>(
            fn: (e: E) => Err<V, RE> | Promise<Err<V, RE>>,
        ): AsyncResult<V, RE>;
        catchAsync<RV = V, RE = E>(
            fn: (e: E) => Result<RV, RE> | Promise<Result<RV, RE>>,
        ): AsyncResult<V | RV, RE>;
        catchAsync<RV = V>(
            fn: (e: E) => NoResult<RV> | Promise<NoResult<RV>>,
        ): AsyncResult<V | RV, E>;
        get(): Promise<Result<V, E>>;
        mapAsync<RV = V>(
            fn: (v: V) => Ok<RV, E> | Promise<Ok<RV, E>>,
        ): AsyncResult<RV, E>;
        mapAsync<RE = E>(
            fn: (v: V) => Err<V, RE> | Promise<Err<V, RE>>,
        ): AsyncResult<V, E | RE>;
        mapAsync<RV = V, RE = E>(
            fn: (v: V) => Result<RV, RE> | Promise<Result<RV, RE>>,
        ): AsyncResult<RV, E | RE>;
        mapAsync<RV = V>(
            fn: (v: V) => NoResult<RV> | Promise<NoResult<RV>>,
        ): AsyncResult<RV, E>;
        matchAsync<RV, RE>(
            ok: (v: V) => RV | Promise<RV>,
            err: (e: E) => RE | Promise<RE>,
        ): Promise<RV | RE>;
        tapAsync(
            fnv: ((v: V) => unknown) | undefined,
            fne?: (e: E) => unknown,
        ): AsyncResult<V, E>;
        unwrapAsync(): Promise<V>;
        unwrapOrAsync<RV = V>(fn: (e: E) => RV | Promise<RV>): Promise<V | RV>;
    }

    Type Parameters

    • V
    • E
    Index

    Methods

    • Transforms the success value if this is an async Ok result, using a function that returns another Result.

      This method is useful to flatten chained async results.

      If this is an async Err result, its error value is preserved unchanged.

      Type Parameters

      • RV = V

      Parameters

      Returns AsyncResult<RV, E>

    • Type Parameters

      • RE = E

      Parameters

      Returns AsyncResult<V, E | RE>

    • Type Parameters

      • RV = V
      • RE = E

      Parameters

      Returns AsyncResult<RV, E | RE>

    • Transforms the success value if this is an async Ok result, using a raw value.

      Example:

      // getCores(): Promise<Result<number, Error>
      // adjustCores(n): Promise<number>
      const r = Res.from(getCores()).mapAsync((n) => adjustCores(n));
      // r: AsyncResult<number, Error>

      If this is an Err result, its error value is preserved unchanged.

      Type Parameters

      • RV = V

      Parameters

      • fn: (v: V) => NoResult<RV> | Promise<NoResult<RV>>

      Returns AsyncResult<RV, E>

    • Exhaustively handles both the async Ok and Err result variants.

      Example:

      const r = await result.match(
      (v) => // Ok branch
      (e) => // Err branch
      )

      Type Parameters

      • RV
      • RE

      Parameters

      • ok: (v: V) => RV | Promise<RV>

        The function called when the result is Ok.

      • err: (e: E) => RE | Promise<RE>

        The function called when the result is Err.

      Returns Promise<RV | RE>

    • Invokes the fnv function with the success value if this is an async Ok result, or the fne function if this is an async Err result.

      The async result is returned unchanged.

      Parameters

      • fnv: ((v: V) => unknown) | undefined
      • Optionalfne: (e: E) => unknown

      Returns AsyncResult<V, E>

    • Returns the success value if this is an async Ok result, or throws if this is an async Err result.

      Returns Promise<V>

      If this is an async Err result, which does not have a value.

    • Returns the success value if this is an async Ok result, or falls back to the value produced by the given function if this is an async Err result.

      Type Parameters

      • RV = V

      Parameters

      • fn: (e: E) => RV | Promise<RV>

      Returns Promise<V | RV>