unthrown icon indicating copy to clipboard operation
unthrown copied to clipboard

Add AsyncResult.Ok / AsyncResult.Err constructors

Open btravers opened this issue 4 months ago • 0 comments

Summary

Add AsyncResult.Ok(value) and AsyncResult.Err(error) constructors (and, if we want the tree-shakeable free-function form, AsyncOk / AsyncErr) so that lifting a pure value into an AsyncResult doesn't require Ok(value).toAsync().

Motivation — real-world evidence

While migrating a large production codebase (~100 files across three layers) from Boxed (@bloodyowl/boxed) to unthrown, Ok(x).toAsync() was by far the most repeated boilerplate:

  • Ok(...).toAsync() — 54 occurrences
  • Err(...).toAsync() — 5 occurrences

Every AsyncResult-returning method that has an early/synchronous branch pays this tax, e.g.:

function loadItems(ids: string[]): AsyncResult<Item[], never> {
  if (ids.length === 0) {
    return Ok([]).toAsync();          // <- everywhere
  }
  return itemRepository.load(ids);
}

The AsyncResult companion already groups the async-producing statics (fromPromise, fromSafePromise, all, allFromDict) but has no Ok/Err.

Proposal

AsyncResult.Ok(3);            // AsyncResult<number, never>
AsyncResult.Err(new E());     // AsyncResult<never, E>
  • Value+type companion, consistent with the existing AsyncResult.* grouping.
  • Semantically identical to Ok(x).toAsync() / Err(e).toAsync(); .toAsync() stays.
  • Optionally also free functions AsyncOk/AsyncErr for the tree-shakeable path (mirrors how Ok/Err are the primary free functions and Result.* is sugar).

Notes / open questions

  • Naming: AsyncResult.Ok/Err (companion-only) vs. also shipping free AsyncOk/AsyncErr. The "one concept, one name" principle suggests these are the same concept as Ok/Err — just pre-lifted — so companion-only may be the cleaner call.
  • Docs: add to the choosing-a-combinator guide next to toAsync.

btravers avatar Jul 08 '26 22:07 btravers