Wraps a function to convert thrown exceptions into Result.Err values.
Executes the provided function and returns Result.Ok on success or Result.Err on thrown exceptions. Supports both synchronous and asynchronous functions.
Function type that returns T or Promise
The return type of the function
Function to execute with exception handling
Result for sync functions, Promise<Result> for async functions
const result = exception2Result(() => { return JSON.parse('invalid json');});// result is Result.Err with parse errorconst asyncResult = await exception2Result(async () => { return await fetch('/api/data');}); Copy
const result = exception2Result(() => { return JSON.parse('invalid json');});// result is Result.Err with parse errorconst asyncResult = await exception2Result(async () => { return await fetch('/api/data');});
Wraps a function to convert thrown exceptions into Result.Err values.
Executes the provided function and returns Result.Ok on success or Result.Err on thrown exceptions. Supports both synchronous and asynchronous functions.