always-panic - v0.10.1
    Preparing search index...

    Class AsyncResult<T, E>

    Type Parameters

    • T
    • E
    Index

    Constructors

    Properties

    promise: Promise<Result<T, E>>

    Methods

    • Enables yield* asyncRes inside an async result.gen / result.genAsync body: awaits the underlying Result, then behaves like its sync counterpart — evaluates to the Ok value or short-circuits the body with the Err. Equivalent to yield* (await asyncRes).

      Returns AsyncGenerator<Err<E>, T>

    • AsyncResult version of Promise.all with fail-fast semantics.

      • Resolves to Ok([...]) once all inputs resolve to Ok (values preserved in input order).
      • Resolves to Err(e) as soon as the first input (in time, not array order) resolves to Err, without waiting for the rest to settle.
      • Rejects if any input's underlying Promise rejects.

      Type Parameters

      • const T extends readonly PromiseLike<Result<unknown, unknown>>[]

      Parameters

      • results: T

      Returns AsyncResult<AsyncResultOkTypes<T>, AsyncResultErrTypes<T>[number]>

      AsyncResult.merge for the variant that waits for every input before picking the first Err by array order.

      const slow = AsyncResult.from(
      new Promise(r => setTimeout(() => r(ok(1)), 1000)),
      )
      const fast = AsyncResult.from(err('boom'))
      const result = await AsyncResult.all([slow, fast])
      expect(result.unwrapErr()).toBe('boom') // resolves ~immediately
    • Waits for every input to settle as a Result, then returns the first Err by array order (or Ok([...]) if none errored).

      Unlike AsyncResult.all, an early Err does not short-circuit the wait. Underlying promise rejections still reject via Promise.all (rejections are not converted to Err values).

      Type Parameters

      • const T extends readonly PromiseLike<Result<unknown, unknown>>[]

      Parameters

      • results: T

        An array of AsyncResults to merge.

      Returns AsyncResult<
          ResultOkTypes<
              { -readonly [P in string
              | number
              | symbol]: Awaited<T[P]> },
          >,
          ResultErrTypes<
              { -readonly [P in string
              | number
              | symbol]: Awaited<T[P]> },
          >[number],
      >

      An AsyncResult that is either an array of all Ok values or the first Err value (by array order).

      AsyncResult.all for fail-fast-on-Err semantics.

      // ok
      const asyncResult1 = AsyncResult.from(ok(1))
      const asyncResult2 = AsyncResult.from(ok("2"))
      const asyncResult3 = AsyncResult.from(ok(3n))
      const merged = await AsyncResult.merge([asyncResult1, asyncResult2, asyncResult3])
      expect(merged.unwrap()).toEqual([1, "2", 3n])
      // err
      const asyncResult1 = AsyncResult.from(ok(1))
      const asyncResult2 = AsyncResult.from(err("2"))
      const asyncResult3 = AsyncResult.from(err(3n))
      const merged = await AsyncResult.merge([asyncResult1, asyncResult2, asyncResult3])
      expect(merged.unwrapErr()).toBe("2")