Safely executes a function, catching any errors.
The return type of the function.
The type of the error.
The function to execute.
Optional
Options for error handling.
A Result containing either the function's return value or the caught error.
const [error, data] = safe(() => JSON.parse('{"foo": "bar"}'));if (error) { console.error(error.message);} else { console.log(data); Copy
const [error, data] = safe(() => JSON.parse('{"foo": "bar"}'));if (error) { console.error(error.message);} else { console.log(data);
Safely executes a function, catching any errors.