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

    Interface PendingOption<T>

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

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

    interface PendingOption<T> {
        and<U>(x: Option<U> | Promise<Option<U>>): PendingOption<Awaited<U>>;
        andThen<U>(
            f: (x: T) => Option<U> | Promise<Option<U>>,
        ): PendingOption<Awaited<U>>;
        catch<R = never>(
            onrejected?: (reason: unknown) => R | PromiseLike<R>,
        ): Promise<Option<T> | R>;
        filter(f: (x: T) => boolean | Promise<boolean>): PendingOption<T>;
        flatten<U>(
            this:
                | PendingOption<Option<U>>
                | PendingOption<PendingOption<U>>
                | PendingOption<PromiseLike<Option<U>>>,
        ): PendingOption<Awaited<U>>;
        inspect(f: (x: T) => unknown): PendingOption<T>;
        iter(): AsyncIterableIterator<Awaited<T>, Awaited<T>, void>;
        map<U>(f: (x: T) => U): PendingOption<Awaited<U>>;
        mapAll<U>(
            f: (x: Option<T>) => Option<U> | Promise<Option<U>>,
        ): PendingOption<Awaited<U>>;
        match<U, F = U>(
            f: (x: T) => U,
            g: () => F,
        ): Promise<Awaited<U> | Awaited<F>>;
        okOr<E>(y: Awaited<E>): PendingResult<T, E>;
        okOrElse<E>(mkErr: () => E | Promise<E>): PendingResult<T, E>;
        or(x: Option<T> | Promise<Option<T>>): PendingOption<Awaited<T>>;
        orElse(f: () => Option<T> | Promise<Option<T>>): PendingOption<Awaited<T>>;
        tap(f: (x: Option<T>) => unknown): PendingOption<T>;
        then<TResult1 = Option<T>, TResult2 = never>(
            onfulfilled?:
                | null
                | ((value: Option) => TResult1 | PromiseLike<TResult1>),
            onrejected?: null | ((reason: any) => TResult2 | PromiseLike<TResult2>),
        ): PromiseLike<TResult1 | TResult2>;
        transpose<U, E>(
            this: PendingOption<Result<U, E>>,
        ): PendingResult<Option<U>, E>;
        xor(y: Option<T> | Promise<Option<T>>): PendingOption<Awaited<T>>;
    }

    Type Parameters

    • T

    Hierarchy (View Summary)

    Index

    Methods

    • Returns a PendingOption with None if this option resolves to None, otherwise returns a PendingOption with x.

      This is the asynchronous version of and.

      • Default: If x is a Promise and rejects, None is returned.
      const x = pendingOption(some(2));
      const y = pendingOption(none<number>());

      expect(await x.and(some(3))).toStrictEqual(some(3));
      expect(await x.and(Promise.resolve(some(3)))).toStrictEqual(some(3));
      expect(await x.and(none())).toStrictEqual(none());
      expect(await x.and(Promise.resolve(none()))).toStrictEqual(none());
      expect(await y.and(some(3))).toStrictEqual(none());
      expect(await y.and(Promise.resolve(none()))).toStrictEqual(none());

      Type Parameters

      • U

      Parameters

      Returns PendingOption<Awaited<U>>

    • Returns a PendingOption with None if this Option resolves to None, otherwise applies f to the resolved value and returns the result.

      This is the asynchronous version of andThen.

      • Default: If f rejects or throws, None is returned.
      const x = pendingOption(some(2));
      const y = pendingOption(none<number>());

      expect(await x.andThen(n => some(n * 2))).toStrictEqual(some(4));
      expect(await x.andThen(n => Promise.resolve(some(n * 2)))).toStrictEqual(some(4));
      expect(await x.andThen(_ => none())).toStrictEqual(none());
      expect(await y.andThen(n => some(n * 2))).toStrictEqual(none());

      Type Parameters

      • U

      Parameters

      Returns PendingOption<Awaited<U>>

    • Returns a PendingOption with None if this option resolves to None, otherwise calls f with the resolved value and returns a PendingOption with the original value if f resolves to true, or None otherwise.

      This is the asynchronous version of filter.

      • Default: If f rejects or throws, None is returned.
      const x = pendingOption(some(2));
      const y = pendingOption(none<number>());

      expect(await x.filter(n => n > 0)).toStrictEqual(some(2));
      expect(await x.filter(n => Promise.resolve(n < 0))).toStrictEqual(none());
      expect(await y.filter(n => true)).toStrictEqual(none());

      Parameters

      Returns PendingOption<T>

    • Calls f with the resolved value if this option is Some, then returns this PendingOption unchanged. Useful for side effects.

      This is the asynchronous version of inspect.

      • Returns a new PendingOption instance with the same value as the original, rather than the exact same reference. The returned option is a distinct object, preserving the original value.
      const x = pendingOption(some(2));
      const y = pendingOption(none<number>());
      let sideEffect = 0;

      expect(await x.inspect(n => (sideEffect = n))).toStrictEqual(some(2));
      expect(sideEffect).toBe(2);
      expect(await y.inspect(n => (sideEffect = n))).toStrictEqual(none());
      expect(sideEffect).toBe(2); // Unchanged

      Parameters

      • f: (x: T) => unknown

      Returns PendingOption<T>

    • Returns an async iterator over this pending option’s value, yielding it if it resolves to Some or nothing if it resolves to None.

      • Yields exactly one item for a resolved Some, or zero items for a resolved None.
      • Compatible with for await...of loops and async spread operators (with caution).
      const x = some(42).toPending();
      const y = none<number>().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 PendingOption with the result if Some, or None if None.

      This is the async version of map.

      • If f throws or rejects, returns None.
      const x = pendingOption(some(2));
      const y = pendingOption(none<number>());

      expect(await x.map(n => n * 2)).toStrictEqual(some(4));
      expect(await x.map(n => Promise.resolve(n * 2))).toStrictEqual(some(4));
      expect(await y.map(n => n * 2)).toStrictEqual(none());

      Type Parameters

      • U

      Parameters

      • f: (x: T) => U

      Returns PendingOption<Awaited<U>>

    • Maps this option by applying a callback to its full state, executing the callback for both Some and None, returning a new PendingOption.

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

      • Default: If f throws or returns a Promise that rejects, the newly created PendingOption will resolve to a None.
      const someOpt = pendingOption(some(42));
      const noneOpt = pendingOption(none<number>());

      const someMapped = someOpt.mapAll(opt => Promise.resolve(some(opt.unwrapOr(0))));
      expect(await someMapped).toStrictEqual(some(42));

      const noneMapped = noneOpt.mapAll(opt => Promise.resolve(some(opt.unwrapOr(0) + 1)));
      expect(await noneMapped).toStrictEqual(some(1));

      Type Parameters

      • U

      Parameters

      Returns PendingOption<Awaited<U>>

    • Matches the resolved option, returning f applied to the value if Some, or g if None. Returns a Promise with the result.

      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 = pendingOption(some(2));
      const y = pendingOption(none<number>());

      expect(await x.match(n => n * 2, () => 0)).toBe(4);
      expect(await y.match(n => n * 2, () => 0)).toBe(0);
      await expect(y.match(n => n * 2, () => { throw new Error() })).rejects.toThrow(OptionError);

      Type Parameters

      • U
      • F = U

      Parameters

      • f: (x: T) => U
      • g: () => F

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

    • Returns this PendingOption if it resolves to Some, otherwise returns a PendingOption with x.

      This is the asynchronous version of or.

      • Default: If x is a Promise that rejects, None is returned.
      const x = pendingOption(some(2));
      const y = pendingOption(none<number>());

      expect(await x.or(some(3))).toStrictEqual(some(2));
      expect(await x.or(Promise.resolve(none()))).toStrictEqual(some(2));
      expect(await y.or(some(3))).toStrictEqual(some(3));
      expect(await y.or(Promise.resolve(none()))).toStrictEqual(none());

      Parameters

      Returns PendingOption<Awaited<T>>

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

      This is the asynchronous version of orElse.

      • Default: If f throws or rejects, None is returned.
      const x = pendingOption(some(2));
      const y = pendingOption(none<number>());

      expect(await x.orElse(() => some(3))).toStrictEqual(some(2));
      expect(await y.orElse(() => Promise.resolve(some(3)))).toStrictEqual(some(3));
      expect(await y.orElse(() => none())).toStrictEqual(none());

      Parameters

      Returns PendingOption<Awaited<T>>

    • Executes f with the resolved option, then returns a new PendingOption 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 = pendingOption(some(42));
      const y = pendingOption(none<number>());
      let log = "";

      expect(await x.tap(opt => (log = opt.toString()))).toStrictEqual(some(42));
      expect(log).toBe("Some { 42 }");
      expect(await y.tap(opt => (log = opt.toString()))).toStrictEqual(none());
      expect(log).toBe("None");

      Parameters

      Returns PendingOption<T>

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

      Type Parameters

      • TResult1 = Option<T>
      • TResult2 = never

      Parameters

      • Optionalonfulfilled: null | ((value: Option) => 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.

    • Returns a PendingOption with Some if exactly one of this option or y resolves to Some, otherwise returns a PendingOption with None.

      This is the asynchronous version of xor.

      • Default: If y is a Promise that rejects, None is returned.
      const x = pendingOption(some(2));
      const y = pendingOption(none<number>());

      expect(await x.xor(some(3))).toStrictEqual(none());
      expect(await x.xor(Promise.resolve(none()))).toStrictEqual(some(2));
      expect(await y.xor(some(3))).toStrictEqual(some(3));
      expect(await y.xor(Promise.resolve(none()))).toStrictEqual(none());

      Parameters

      Returns PendingOption<Awaited<T>>