unthrown
unthrown copied to clipboard
No async / dynamic-array result combinator (`allAsync` + array overload of `all`)
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:
- No async version. There's no
allAsync/AsyncResult.all. To combine a list ofAsyncResults you must resolve them yourself first. - Tuple-only typing. The
readonly [...Rs]signature is great for fixed tuples but doesn't infer toResult<T[], E>for a dynamicResult<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
- Add
allAsync(results: readonly AsyncResult<T, E>[]): AsyncResult<T[], E>— fail-fast on the firstErr, with aDefectdominating. (Resolution can be concurrent; ordering of the resolvedResults is preserved.) - Add an array overload to the existing sync
allsoResult<T, E>[]infers toResult<T[], E>without a cast (keep the tuple overload for fixed-length inputs).