Function: tryCatch()
Call Signature
tryCatch<
TryReturn,CatchReturn>(toTry,toCatch):Result<TryReturn|UnwrapOk<CatchReturn>,InferValueAsErr<CatchReturn>>
Defined in: index.ts:569
Executes a function or value safely, catching any errors and returning a Result. Unlike fromThrowable, this executes the operation immediately rather than returning a wrapped function.
Type Parameters
TryReturn
TryReturn
The type returned by the try operation
CatchReturn
CatchReturn
The type returned by the catch handler
Parameters
toTry
() => NonPromise<TryReturn>
The function to execute or value to resolve
toCatch
(error) => NonPromise<CatchReturn>
Value or function to handle caught errors
Returns
Result<TryReturn | UnwrapOk<CatchReturn>, InferValueAsErr<CatchReturn>>
A Result containing either the success value or the catch value
Example
tryCatch(
() => 10 / 0,
(error) => `Parse failed: ${String(error)}`
); // Result<number, `Parse failed: ${string}`>
tryCatch(
() => { throw new Error("Something went wrong"); },
"Default error message"
); // Result<never, "Default error message">
tryCatch(
() => fetch('/api/data').then(r => r.json()),
(error) => ({ message: "Network error" })
); // Promise<Result<any, { readonly message: "Network error" }>>Call Signature
tryCatch<
TryReturn,CatchReturn>(toTry,toCatch):Result<TryReturn|UnwrapOk<CatchReturn>,InferValueAsErr<CatchReturn>>
Defined in: index.ts:573
Executes a function or value safely, catching any errors and returning a Result. Unlike fromThrowable, this executes the operation immediately rather than returning a wrapped function.
Type Parameters
TryReturn
TryReturn
The type returned by the try operation
CatchReturn
CatchReturn
The type returned by the catch handler
Parameters
toTry
() => NonPromise<TryReturn>
The function to execute or value to resolve
toCatch
NonPromise<CatchReturn>
Value or function to handle caught errors
Returns
Result<TryReturn | UnwrapOk<CatchReturn>, InferValueAsErr<CatchReturn>>
A Result containing either the success value or the catch value
Example
tryCatch(
() => 10 / 0,
(error) => `Parse failed: ${String(error)}`
); // Result<number, `Parse failed: ${string}`>
tryCatch(
() => { throw new Error("Something went wrong"); },
"Default error message"
); // Result<never, "Default error message">
tryCatch(
() => fetch('/api/data').then(r => r.json()),
(error) => ({ message: "Network error" })
); // Promise<Result<any, { readonly message: "Network error" }>>Call Signature
tryCatch<
TryReturn,CatchReturn>(toTry,toCatch):Promise<Result<Awaited<TryReturn> |UnwrapOk<CatchReturn>,InferValueAsErr<CatchReturn>>>
Defined in: index.ts:577
Executes a function or value safely, catching any errors and returning a Result. Unlike fromThrowable, this executes the operation immediately rather than returning a wrapped function.
Type Parameters
TryReturn
TryReturn extends unknown
The type returned by the try operation
CatchReturn
CatchReturn
The type returned by the catch handler
Parameters
toTry
OrFunction<TryReturn>
The function to execute or value to resolve
toCatch
OrFunction<OrPromise<CatchReturn>, [unknown]>
Value or function to handle caught errors
Returns
Promise<Result<Awaited<TryReturn> | UnwrapOk<CatchReturn>, InferValueAsErr<CatchReturn>>>
A Result containing either the success value or the catch value
Example
tryCatch(
() => 10 / 0,
(error) => `Parse failed: ${String(error)}`
); // Result<number, `Parse failed: ${string}`>
tryCatch(
() => { throw new Error("Something went wrong"); },
"Default error message"
); // Result<never, "Default error message">
tryCatch(
() => fetch('/api/data').then(r => r.json()),
(error) => ({ message: "Network error" })
); // Promise<Result<any, { readonly message: "Network error" }>>