Function: tryCatch()
Call Signature
tryCatch<
T>(fn):Result<T,unknown>
Defined in: result/constructors/index.ts:56
Wraps a potentially throwing function in a Result.
If the function executes successfully, returns Ok with the value. If it throws, returns Err with the error.
Type Parameters
T
T
Parameters
fn
() => T
Function that may throw
Returns
Result<T, unknown>
Result containing either the return value or the error
Example
const parseJSON = (str: string) =>
tryCatch(() => JSON.parse(str))
parseJSON('{"name":"John"}')
// => Ok({ name: 'John' })
parseJSON('invalid json')
// => Err(SyntaxError: ...)
Call Signature
tryCatch<
T,E>(fn,mapError):Result<T,E>
Defined in: result/constructors/index.ts:78
Wraps a potentially throwing function in a Result with custom error mapping.
Type Parameters
T
T
E
E
Parameters
fn
() => T
Function that may throw
mapError
(error) => E
Function to transform the caught error
Returns
Result<T, E>
Result containing either the return value or the mapped error
Example
const parseNumber = (str: string) =>
tryCatch(
() => {
const n = Number(str)
if (Number.isNaN(n)) throw new Error('Invalid number')
return n
},
(e) => `Parse error: ${e}`
)