go-go-try
go-go-try copied to clipboard
Tries to execute a sync/async function, returns a Golang style result
trafficstars
go-go-try
Tries to execute a sync/async function, returns a Golang style result.
Why
Why not nice-try with it's 70+ million downloads per month?
go-go-trysupports async functions.go-go-tryallows you to capture the thrown error.go-go-tryis written in TypeScript. The types are written in a way that reduce developer errors.go-go-tryis inspired by Golang error catching.
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.
Install
npm install go-go-try
Usage
import { goTry, goExpect, 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())
sentToErrorTrackingService(err)
// if you still want the task to throw but with a optional custom message
const value = await goExpect(() => JSON.parse('{/}', (err) => `Malformed JSON!`)) // value will be always T
// goTry and goExpect extract the error message from the error object, if you want the raw error object, use goTryRaw
const [err, value] = await goTryRaw(() => JSON.parse('{/}')) // err will be unknown, value will be always T
API
First parameter accepts:
- synchronous function
goTry(() => JSON.parse(value)) - asynchronous function / Promise
Returns a tuple with the possible error and result (Golang style)
If you use TypeScript, the types are well defined and won't let you make a mistake.
Inspiration
- This library started as a fork of good-try but diverged a lot so I decided to rename it