@ts-rust/std - v0.0.5
    Preparing search index...

    Interface PendingResult<T, E>

    Interface defining an asynchronous Result that wraps a Promise resolving to a Result.

    Extends Result functionality for pending states, with methods mirroring their synchronous counterparts but returning PendingResult or Promise for async operations. Rejections typically resolve to Err unless otherwise specified.

    interface PendingResult<T, E> {
        and<U>(
            x: Result<U, E> | Promise<Result<U, E>>,
        ): PendingResult<Awaited<U>, Awaited<E>>;
        andThen<U>(
            f: (x: T) => Result<U, E> | Promise<Result<U, E>>,
        ): PendingResult<Awaited<U>, Awaited<E>>;
        catch<R = never>(
            onrejected?: (reason: unknown) => R | PromiseLike<R>,
        ): Promise<Result<T, E> | R>;
        check(): Promise<readonly [boolean, Awaited<T> | CheckedError<Awaited<E>>]>;
        err(): PendingOption<Awaited<E>>;
        flatten<U, F>(
            this:
                | PendingResult<Result<U, F>, F>
                | PendingResult<PendingResult<U, F>, F>
                | PendingResult<PromiseLike<Result<U, F>>, F>,
        ): PendingResult<Awaited<U>, Awaited<F>>;
        inspect(f: (x: T) => unknown): PendingResult<T, E>;
        inspectErr(f: (x: CheckedError<E>) => unknown): PendingResult<T, E>;
        iter(): AsyncIterableIterator<Awaited<T>, Awaited<T>, void>;
        map<U>(f: (x: T) => U): PendingResult<Awaited<U>, Awaited<E>>;
        mapAll<U, F>(
            f: (x: Result<T, E>) => Result<U, F> | Promise<Result<U, F>>,
        ): PendingResult<Awaited<U>, Awaited<F>>;
        mapErr<F>(f: (x: E) => F): PendingResult<Awaited<T>, Awaited<F>>;
        match<U, F = U>(
            f: (x: T) => U,
            g: (e: CheckedError<E>) => F,
        ): Promise<Awaited<U> | Awaited<F>>;
        or<F>(
            x: Result<T, F> | Promise<Result<T, F>>,
        ): PendingResult<Awaited<T>, Awaited<F>>;
        orElse<F>(
            f: () => Result<T, F> | Promise<Result<T, F>>,
        ): PendingResult<Awaited<T>, Awaited<F>>;
        tap(f: (x: Result<T, E>) => unknown): PendingResult<T, E>;
        then<TResult1 = Result<T, E>, TResult2 = never>(
            onfulfilled?:
                | null
                | ((value: Result) => TResult1 | PromiseLike<TResult1>),
            onrejected?: null | ((reason: any) => TResult2 | PromiseLike<TResult2>),
        ): PromiseLike<TResult1 | TResult2>;
        transpose<U, F>(
            this: PendingResult<Option<U>, F>,
        ): PendingOption<Result<U, F>>;
        try(): Promise<
            readonly [
                boolean,
                undefined
                | CheckedError<Awaited<E>>,
                undefined | Awaited<T>,
            ],
        >;
    }

    Type Parameters

    • T
    • E

    Hierarchy (View Summary)

    Index

    Methods

    • Inspects this PendingResult’s state, returning a promise of a tuple with a success flag and either the value or error.

      This is the asynchronous version of check.

      • Resolves to [true, Awaited<T>] if this is an Ok, or to [false, CheckedError<Awaited<E>>] if this is an Err.
      • Never rejects, providing a safe way to await the result’s state.
      const x = ok<number, string>(42).toPending();
      const y = err<number, string>("failure").toPending();

      expect(await x.check()).toEqual([true, 42]);
      expect(await y.check()).toEqual([false, expect.objectContaining({ expected: "failure" })]);

      Returns Promise<readonly [boolean, Awaited<T> | CheckedError<Awaited<E>>]>

    • Calls f with the value if this pending result resolves to an Ok, then returns a new pending result with the original state.

      This is the asynchronous version of inspect.

      • Returns a new PendingResult instance, not the original reference.
      • If f throws or returns a Promise that rejects, the error is ignored, and the returned promise still resolves to the original state.
      const x = ok<number, string>(2).toPending();
      const y = err<number, string>("failure").toPending();
      let sideEffect = 0;

      expect(await x.inspect(n => (sideEffect = n))).toStrictEqual(ok(2));
      expect(await x.inspect(_ => { throw new Error() })).toStrictEqual(ok(2));
      expect(sideEffect).toBe(2);
      expect(await y.inspect(n => (sideEffect = n))).toStrictEqual(err("failure"));
      expect(sideEffect).toBe(2); // Unchanged

      Parameters

      • f: (x: T) => unknown

      Returns PendingResult<T, E>

    • Calls f with the error if this pending result resolves to an Err, then returns a new pending result with the original state.

      This is the asynchronous version of inspectErr.

      • Returns a new PendingResult instance, not the original reference.
      • If f throws or returns a Promise that rejects, the error is ignored, and the returned promise still resolves to the original state.
      const x = ok<number, string>(2).toPending();
      const y = err<number, string>("failure").toPending();
      let sideEffect = 0;

      expect(await x.inspectErr(n => (sideEffect = n))).toStrictEqual(ok(2));
      expect(await x.inspectErr(_ => { throw new Error() })).toStrictEqual(ok(2));
      expect(sideEffect).toBe(0);
      expect(await y.inspectErr(n => (sideEffect = n))).toStrictEqual(err("failure"));
      expect(await y.inspectErr(_ => { throw new Error() })).toStrictEqual(err("failure"));
      expect(sideEffect).toBe(2);

      Parameters

      Returns PendingResult<T, E>

    • Returns an async iterator over this pending result’s value, yielding it if it resolves to Ok or nothing if it resolves to Err.

      • Yields exactly one item for a resolved Ok, or zero items for a resolved Err.
      • Compatible with for await...of loops and async spread operators (with caution).
      • Ignores the error value in Err cases, focusing only on the success case.
      const x = ok<number, string>(42).toPending();
      const y = err<number, string>("failure").toPending();

      const iterX = x.iter();
      expect(await iterX.next()).toEqual({ value: 42, done: false });
      expect(await iterX.next()).toEqual({ done: true });

      const iterY = y.iter();
      expect(await iterY.next()).toEqual({ done: true });

      async function collect(iter) {
      const result = [];
      for await (const val of iter) result.push(val);
      return result;
      }
      expect(await collect(x.iter())).toEqual([42]);
      expect(await collect(y.iter())).toEqual([]);

      Returns AsyncIterableIterator<Awaited<T>, Awaited<T>, void>

    • Maps the resolved value with f, returning a PendingResult with the result if Ok, or the original Err if Err.

      This is the asynchronous version of map.

      • If f throws or returns a rejected promise, returns a PendingResult with an Err containing an UnexpectedError.
      const x = ok<number, string>(2).toPending();
      const y = err<number, string>("failure").toPending();

      expect(await x.map(n => n * 2)).toStrictEqual(ok(4));
      expect(await x.map(() => { throw new Error("boom") }).unwrapErr().unexpected).toBeDefined();
      expect(await y.map(n => n * 2)).toStrictEqual(err("failure"));

      Type Parameters

      • U

      Parameters

      • f: (x: T) => U

      Returns PendingResult<Awaited<U>, Awaited<E>>

    • Maps this pending result by applying a callback to its full state, executing the callback for both Ok and Err, returning a new PendingResult.

      Unlike andThen, which only invokes the callback for Ok, this method always calls f, passing the entire Result as its argument.

      This is the asynchronous version of mapAll.

      • If f throws or returns a Promise that rejects, the newly created PendingResult will resolve to an Err with an UnexpectedError.
      const okRes = ok<number, string>(42).toPending();
      const errRes = err<number, string>("failure").toPending();

      const okMapped = okRes.mapAll(res => Promise.resolve(ok(res.unwrapOr(0) + 1)));
      expect(await okMapped).toStrictEqual(ok(43));

      const errMapped = errRes.mapAll(res => Promise.resolve(ok(res.unwrapOr(0) + 1)));
      expect(await errMapped).toStrictEqual(ok(1));

      const throwMapped = okRes.mapAll(() => { throw new Error("boom") });
      expect(await throwMapped.unwrapErr().unexpected).toBeDefined();

      Type Parameters

      • U
      • F

      Parameters

      Returns PendingResult<Awaited<U>, Awaited<F>>

    • Transforms this pending result by applying f to the error if it resolves to an Err with an expected error, or preserves the Ok unchanged.

      This is the asynchronous version of mapErr.

      • If f throws or returns a rejected promise, returns a PendingResult with an Err containing an UnexpectedError.
      • If this resolves to an Err with an UnexpectedError, f is not called, and the original error is preserved.
      const x = ok<number, string>(2).toPending();
      const y = err<number, string>("failure").toPending();

      expect(await x.mapErr(e => e.length)).toStrictEqual(ok(2));
      expect(await y.mapErr(e => e.length)).toStrictEqual(err(7));
      expect(await y.mapErr(() => { throw new Error("boom") }).unwrapErr().unexpected).toBeDefined();

      Type Parameters

      • F

      Parameters

      • f: (x: E) => F

      Returns PendingResult<Awaited<T>, Awaited<F>>

    • Matches this PendingResult, returning a promise of f applied to the value if it resolves to an Ok, or g applied to the error if it resolves to an Err.

      This is the asynchronous version of match.

      • If f or g throws or returns a rejected Promise, the returned promise rejects with the original error. In this case the caller is responsible for handling the rejection.
      const x = ok<number, string>(2).toPending();
      const y = err<number, string>("failure").toPending();

      expect(await x.match(n => n * 2, () => 0)).toBe(4);
      expect(await y.match(n => n * 2, e => e.expected.length)).toBe(7);

      Type Parameters

      • U
      • F = U

      Parameters

      Returns Promise<Awaited<U> | Awaited<F>>

    • Returns this pending result if it resolves to an Ok, otherwise returns x.

      This is the asynchronous version of or.

      • If this result resolves to an Err and x is a Promise that rejects, the resulting PendingResult resolves to an Err with an UnexpectedError.
      const x = ok<number, string>(2).toPending();
      const y = err<number, string>("failure").toPending();

      expect(await x.or(ok(3))).toStrictEqual(ok(2));
      expect(await x.or(err("another one"))).toStrictEqual(ok(2));
      expect(await y.or(ok(3))).toStrictEqual(ok(3));
      expect(await y.or(err("another one"))).toStrictEqual(err("failure"));
      expect(await y.or(Promise.reject(new Error("boom"))).unwrapErr().unexpected).toBeDefined();

      Type Parameters

      • F

      Parameters

      Returns PendingResult<Awaited<T>, Awaited<F>>

    • Returns this PendingResult if it resolves to Ok, otherwise returns a PendingResult with the result of f.

      This is the asynchronous version of orElse.

      • If f throws or returns a rejected promise, the resulting PendingResult resolves to an Err with an UnexpectedError.
      const x = ok<number, string>(2).toPending();
      const y = err<number, string>("failure").toPending();

      expect(await x.orElse(() => ok(3))).toStrictEqual(ok(2));
      expect(await y.orElse(() => Promise.resolve(ok(3)))).toStrictEqual(ok(3));
      expect(await y.orElse(() => { throw new Error("boom") }).unwrapErr().unexpected).toBeDefined();
      expect(await y.orElse(() => err("another one"))).toStrictEqual(err("another one"));

      Type Parameters

      • F

      Parameters

      Returns PendingResult<Awaited<T>, Awaited<F>>

    • Executes f with the resolved result, then returns a new PendingResult unchanged.

      This is the asynchronous version of tap.

      • If f throws or rejects, the error is ignored
      • If f returns a promise, the promise is not awaited before returning
      const x = pendingResult(ok<number, string>(42));
      const y = pendingResult(err<number, string>("failure"));
      let log = "";

      expect(await x.tap(res => (log = res.toString()))).toStrictEqual(ok(42));
      expect(log).toBe("Ok { 42 }");
      expect(await y.tap(res => (log = res.toString()))).toStrictEqual(err("failure"));
      expect(log).toBe("Err { 'failure' }");

      Parameters

      Returns PendingResult<T, E>

    • Attaches callbacks for the resolution and/or rejection of the Promise.

      Type Parameters

      Parameters

      • Optionalonfulfilled: null | ((value: Result) => TResult1 | PromiseLike<TResult1>)

        The callback to execute when the Promise is resolved.

      • Optionalonrejected: null | ((reason: any) => TResult2 | PromiseLike<TResult2>)

        The callback to execute when the Promise is rejected.

      Returns PromiseLike<TResult1 | TResult2>

      A Promise for the completion of which ever callback is executed.

    • Extracts this PendingResult’s state, returning a promise of a tuple with a success flag, error, and value.

      Inspired by the Try Operator proposal.

      This is the asynchronous version of try.

      • Resolves to [true, undefined, Awaited<T>] if this is an Ok, or [false, CheckedError<Awaited<E>>, undefined] if this is an Err.
      • Never rejects, offering a safe way to await the result’s state with explicit success indication.
      const x = ok<number, string>(42).toPending();
      const y = err<number, string>("failure").toPending();

      expect(await x.try()).toEqual([true, undefined, 42]);
      expect(await y.try()).toEqual([false, expect.objectContaining({ expected: "failure" }), undefined]);

      Returns Promise<
          readonly [
              boolean,
              undefined
              | CheckedError<Awaited<E>>,
              undefined | Awaited<T>,
          ],
      >