unthrown
unthrown copied to clipboard
Add AsyncResult.Ok / AsyncResult.Err constructors
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 occurrencesErr(...).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/AsyncErrfor the tree-shakeable path (mirrors howOk/Errare the primary free functions andResult.*is sugar).
Notes / open questions
- Naming:
AsyncResult.Ok/Err(companion-only) vs. also shipping freeAsyncOk/AsyncErr. The "one concept, one name" principle suggests these are the same concept asOk/Err— just pre-lifted — so companion-only may be the cleaner call. - Docs: add to the choosing-a-combinator guide next to
toAsync.