@replygirl/tc

destructurable, async-friendly `try...catch` wrapper function with support for error side effects and fallback values


Keywords
async, await, awaitable, promise, try, catch, wrapper, function, fallback, error handling, destructure, destructuring, functional, utility, helper, async-await, error-handling, es6, javascript, nodejs, try-catch, typescript
License
ISC
Install
npm install @replygirl/tc@4.0.2

Documentation

tc

node-current (scoped) GitHub top language Libraries.io dependency status for latest release, scoped npm package Maintainability Test Coverage GitHub issues GitHub pull requests

destructurable, async-friendly try...catch wrapper function with support for error side effects and fallback values

Installation & usage

See the docs for full details.

import tc from '@replygirl/tc'

// get the return value and error of a try callback
const [x, e] = tc(() => { /* ... */ })

// or put complex error handling in a catch callback
const [y] = await tc(doSomething, async e => {
  await reportError(e)
  return getFallbackValue(e)
})

// reuse your error handling by defining your own wrapper
const tce = (t, c) => tc(t, async e => {
  await reportError(e)
  return c(e)
})
const [z] = tce(doSomething, getFallbackValue)

ISC License | Copyright © 2020–present replygirl