attempt
Executes a synchronous function and wraps the outcome in a Result, so callers can handle failure without try/catch.
Usage
attempt(fn)
attempt is the canonical way to call a function that may throw — JSON parsing, schema validation, anything from a third-party API that doesn't itself return a Result. The thrown value is normalized to an Error (strings get wrapped; non-error values become an Error whose message is JSON.stringify(thrown) and whose cause is the original value).
Parameters
fn() => Trequired
the function to execute. Must be synchronous — use attemptAsync for async work.
Returns
an Ok<T> on success, Err on throw.
Examples
Replace try/catch at the boundary of an unsafe API:
See also
attemptAsync— async variantok/err— construct results manuallyunwrap— extract or throwP.ok/P.err— patterns formatch- Result & Errors concept guide