Converts a function that may throw an error into a function that returns a Result.
The arguments of the function.
The return type of the function.
A string literal type for the error label.
The function that may throw an error.
The error label to use if the function throws.
A new function that wraps the original function and returns a Result.
const safeJsonParse = fromThrowable(JSON.parse, 'json_parse_error');const [error, data] = safeJsonParse('{"key": "value"}');if (error) { // Handle parsing error} else { // Use data} Copy
const safeJsonParse = fromThrowable(JSON.parse, 'json_parse_error');const [error, data] = safeJsonParse('{"key": "value"}');if (error) { // Handle parsing error} else { // Use data}
Converts a function that may throw an error into a function that returns a Result.