unthrown icon indicating copy to clipboard operation
unthrown copied to clipboard

No async / dynamic-array result combinator (`allAsync` + array overload of `all`)

Open btravers opened this issue 5 months ago • 0 comments

Summary

all is the only result combinator, and it's (a) sync-only — it doesn't accept AsyncResults — and (b) tuple-typed, so it doesn't reduce cleanly over a homogeneous array of unknown length. Combining N results is a table-stakes primitive; today it requires a hand-rolled helper plus a cast.

Where

declare function all<Rs extends readonly Result<unknown, unknown>[]>(
  results: readonly [...Rs]
): Result<{ [K in keyof Rs]: OkOf<Rs[K]> }, ErrOf<Rs[number]>>;

Two gaps:

  1. No async version. There's no allAsync / AsyncResult.all. To combine a list of AsyncResults you must resolve them yourself first.
  2. Tuple-only typing. The readonly [...Rs] signature is great for fixed tuples but doesn't infer to Result<T[], E> for a dynamic Result<T, E>[], so a cast is needed.

Real-world impact

A consumer combining a dynamic list of AsyncResults had to write this helper:

function combineAsync<T, E>(results: readonly AsyncResult<T, E>[]): AsyncResult<T[], E> {
  const resolveAll: Promise<Result<T, E>[]> = Promise.all(results);
  return fromSafePromise(resolveAll).flatMap((resolved) => all(resolved) as Result<T[], E>);
  //                                                        ^ cast forced because `all` is tuple-only
}

Both the helper and the as cast would disappear with first-class support.

Proposal

  1. Add allAsync(results: readonly AsyncResult<T, E>[]): AsyncResult<T[], E> — fail-fast on the first Err, with a Defect dominating. (Resolution can be concurrent; ordering of the resolved Results is preserved.)
  2. Add an array overload to the existing sync all so Result<T, E>[] infers to Result<T[], E> without a cast (keep the tuple overload for fixed-length inputs).

btravers avatar Jun 26 '26 14:06 btravers