Tries to execute a sync/async function, returns a Golang style result.
- Supports sync/async functions.
- Allows you to capture the thrown error.
- Written in TypeScript. The types are written in a way that reduce developer errors.
- Inspired by Golang error catching.
- Zero dependencies.
Why not just try/catch?
- In a lot of cases,
try/catchis still the better option. - Nested
try/catchstatements are hard to process mentally. They also indent the code and make it hard to read. A singletry/catchdoes the same but to a lesser degree. - If you prefer const,
try/catchstatements get in the way because you need to useletif you need the variable outside of thetry/catchscope:let todos try { todos = JSON.parse(localStorage.getItem('todos')) } catch {} return todos.filter((todo) => todo.done)
- It takes more space. It's slower to type.
npm install go-go-tryimport { goTry, goTryRaw } from 'go-go-try'
// tries to parse todos, returns empty array if it fails
const [_, value = []] = goTry(() => JSON.parse(todos))
// fetch todos, on error, fallback to empty array
const [_, todos = []] = await goTry(fetchTodos())
// fetch todos, fallback to empty array, send error to your error tracking service
const [err, todos = []] = await goTry(fetchTodos()) // err is string | undefined
if (err) sendToErrorTrackingService(err)
// goTry extracts the error message from the error object, if you want the raw error object, use goTryRaw
const [err, value] = goTryRaw(() => JSON.parse('{/}')) // err is Error | undefined, value is T | undefined
// fetch todos, fallback to empty array, send error to your error tracking service
const [err, todos = []] = await goTryRaw(fetchTodos()) // err is Error | undefined
if (err) sendToErrorTrackingService(err)First parameter accepts:
- synchronous/asynchronous function / Promise
Returns a tuple with the possible error and result as [Err | undefined, T | undefined] (Golang style)
If you use TypeScript, the types are well defined and won't let you make a mistake.