safelyTry() is a type-safe elegant try-catch replacement for JavaScript / TypeScript inspired by Go error handling.
npm i --save safely-tryInstead of using ugly native try-catch like this
let result;
try {
result = somethingMightThrowException();
} catch(e) {
handleTheError(e);
}you can use safelyTry to do something like this
import safelyTry from 'safely-try';
const { data: result, error } = safelyTry(somethingMightThrowException);
if (error) {
handleTheError(error);
return;
}or you can use safelyTryTuple to do something like this
import { safelyTryTuple as st } from 'safely-try';
const [result, error] = st(somethingMightThrowException);
if (error) {
handleTheError(error);
return;
}Type inference works with Language Server Protocol (LSP) in your favorite IDE
Two screenshots were taken from Visual Studio Code and combined with Nano Banana Pro
