@bodil/opt
    Preparing search index...

    Variable Result

    Result: {
        await: {
            <A, E extends Error>(m: Promise<A>): Promise<Result<A, E>>;
            <A, E extends Error>(m: PromiseLike<A>): PromiseLike<Result<A, E>>;
        };
        Class: typeof Result;
        fromJSON<A, E>(doc: { result: boolean; value: A | E }): Result<A, E>;
        is(value: unknown): value is Result<unknown, unknown>;
        lift<A, F extends (...args: any[]) => A, E = unknown>(
            fn: F,
        ): (...args: Parameters<F>) => Result<A, E>;
        try<A, E = unknown>(f: () => A): Result<A, E>;
    }

    Static methods on the Result object.

    Type Declaration

    • await: {
          <A, E extends Error>(m: Promise<A>): Promise<Result<A, E>>;
          <A, E extends Error>(m: PromiseLike<A>): PromiseLike<Result<A, E>>;
      }

      Convert a Promise returning A into a Promise returning a Result of either A or the Error type. The new Promise always succeeds, reflecting an error condition in the Result instead of the failure callback.

      const fetchResult = await Result.await(fetch("https://example.com/example.txt"));
      if (fetchResult.isErr()) {
      console.error(fetchResult.value.message);
      }
    • Class: typeof Result

      The class constructor of Results. You should never use this to construct Results directly, preferring instead Ok and Err. It's exposed for use in instanceof checks, though calling Result.is to decide resultiness is preferred.

      import { expect } from "chai";
      expect(Ok("stop dots")).to.be.an.instanceof(Result.Class);
    • fromJSON: function
    • is: function
      • Test whether an unknown value is a Result.

        Parameters

        • value: unknown

        Returns value is Result<unknown, unknown>

        function assertResult(value: unknown): void {
        if (Result.is(value)) {
        value.assertOk();
        }
        }
    • lift: function
      • Turn a function that returns A and potentially throws E into a function that catches E if thrown and returns Result<A, E>.

        Beware that TypeScript's type inference isn't currently very good at this, so you should explicitly provide the target function signature when using this function, or you're likely to end up with a Result<unknown, unknown> instead of the expected Result<A, E>.

        Type Parameters

        • A
        • F extends (...args: any[]) => A
        • E = unknown

        Parameters

        • fn: F

        Returns (...args: Parameters<F>) => Result<A, E>

        function div(n: number, by: number): number {
        if (by === 0) {
        throw new RangeError("division by zero");
        }
        return n / by;
        }
        const liftedDiv: (n: number, by: number) => Result<number, RangeError> =
        Result.lift(div);
    • try: function
      • Run a function and return its result as an Ok if it didn't throw any exceptions, or an Err containing the thrown exception.

        Type Parameters

        • A
        • E = unknown

        Parameters

        • f: () => A

        Returns Result<A, E>

        const tryFn: Result<number, Error> = Result.try(() => {
        throw new Error("fix the bug!");
        });

        if (tryFn.isErr()) {
        console.error(tryFn.value.message);
        }