attemptAsync
Executes an asynchronous function and wraps the outcome in a Result. Awaits the returned promise — Err carries the rejection reason.
Usage
attemptAsync(fn)
Use attemptAsync for any Promise-returning operation that may reject: HTTP fetches, database calls, file I/O. The rejection value is normalized to an Error the same way attempt normalizes thrown values.
Parameters
fn() => Promise<T>required
a function returning a promise. Pass a thunk, not a promise — attemptAsync(promise) would not catch a synchronous throw inside the producing expression.
Returns
resolves to Ok<T> if the promise fulfills, Err if it rejects. Never rejects.
Examples
Compose with match at a route boundary:
See also
attempt— synchronous variantP.ok/P.err— patterns formatch- Result & Errors concept guide