factories
{/* @generated by scripts/sync-content.mjs; do not edit */}
@sandlada/result/factories — constructors that turn values, predicates, throws and Promises into Results.
- Operates on:
IResultOfTconstruction (sync) andPromise<IResultOfT>construction (eager). - Execution model: synchronous;
fromPromise,fromSafePromise,tryCatchAsync,asyncOkandasyncErrreturn in-flight Promises. - Not here: transformations (
operators,promise-result), Option constructors (option).
Synchronous constructors
Section titled “Synchronous constructors”| Export | Description | Source |
|---|---|---|
ok |
Success without a value (IResult<never>) or with value (IResultOfT<T, never>); the error channel starts as never so it widens at the use site. |
ok.ts |
err |
Failure carrying error; the error type is inferred from the argument and the value channel starts as never. |
err.ts |
fromPredicate |
Ok(value) when the predicate passes, otherwise Err(error). |
fromPredicate.ts |
fromThrowable |
Wraps a possibly-throwing function into a Result-returning one. | fromThrowable.ts |
tryCatch |
Runs a thunk and captures a synchronous throw as Err. |
tryCatch.ts |
Eager async constructors
Section titled “Eager async constructors”| Export | Description | Source |
|---|---|---|
asyncOk |
Pre-resolved success Promise<IResultOfT<T, never>>. |
asyncOk.ts |
asyncErr |
Pre-resolved failure Promise<IResultOfT<never, E>>. |
asyncErr.ts |
fromPromise |
Wraps a Promise; a rejection becomes Err instead of staying a rejection. |
fromPromise.ts |
fromSafePromise |
Wraps a Promise that is known never to reject; E defaults to Error. |
fromSafePromise.ts |
tryCatchAsync |
Runs an async thunk; throws and rejections become Err. |
tryCatchAsync.ts |
Contract notes
Section titled “Contract notes”okanderrreturn the narrowest variant (IResultOfT<T, never>/IResultOfT<never, E>). Use the dual-parameter overloadsok<T, E>(value)/err<T, E>(error)to widen without a cast when the surrounding context already declares a wider channel.- Every capture helper turns throws and rejections into values; none of them rejects.
fromSafePromisenormalizes a non-Errorrejection value tonew Error(String(e))when noerrorFnis supplied; passerrorFnto keep your own error type.- Throw policy: behavior-modes.md §4.1.
Design notes
Section titled “Design notes”- Generic
TError, defaulting tounknown. Constructors never coerce a failure intoError:errkeeps the caller’s error type, andokleaves the error channel atneveruntil it widens at the use site.
Related
Section titled “Related”operators— transforms the Results these factories build.promise-result— operates on the eager Promises these factories return.async-result— lazy thunks as an alternative async carrier.