$try(fn)
$try is another util that allows us quickly convert code that throws exceptions into code that returns result types.
Wraps any synchronous function that returns a raw value and converts it into a Result.
Example
ts
const res = $try(() => anyRandomFunction())This will execute the function catching any errors and returning an Err object. If the code doesn't throw it returns the normal output of the function in an Ok object with $code.ok.
Sig
ts
$try(() => T): Result<T, E = unknown>
$atry(() => Promise<T>): Promise<Result<T, E = unknown>>$atry(fn)
Same as $try, but for async functions.
Sig
ts
$atry(() => Promise<T>) → Promise<Result<T, E>>