Runs a function and returns a Result. If the function succeeds, the Ok is returned. If the function fails, the Err is returned.
The type of the value.
The type of the error.
The function to be run.
A Result that is the result of running the given function.
const result = Result.run(() => { if (Math.random() > 0.5) { return 'Success'; } else { throw new Error('Failure'); }});if (result.isOk()) { console.log(result.unwrap()); // 'Success'} else { console.error(result.unwrapError()); // Error: 'Failure'} Copy
const result = Result.run(() => { if (Math.random() > 0.5) { return 'Success'; } else { throw new Error('Failure'); }});if (result.isOk()) { console.log(result.unwrap()); // 'Success'} else { console.error(result.unwrapError()); // Error: 'Failure'}
Runs a function and returns a Result. If the function succeeds, the Ok is returned. If the function fails, the Err is returned.