@fischi20/errval
v0.0.3
Published
Why try-catch if you can await safe?
Downloads
2
Readme
Errval
Errval is a simple error handling library for JS/TS. It encourages to use Errors as values over exceptions.
Installation
# npm
npm i @fischi20/errval
# pnpm
pnpm i @fischi20/errval
# yarn
yarn add @fischi20/errval
Usage
safe await a fetch request
import { safe } from '@fischi20/errval' const [response, error] = await safe(fetch('https://example.com/data'))
safe await a fetch callback
import { safe } from '@fischi20/errval' const [response, error] = await safe(() => fetch('https://example.com/data'))
secure a function
import { secure } from '@fischi20/errval' const securedDivision = secure((a: number, b: number) => { if (b === 0) { throw new Error('division by zero') } return a / b })
results
import { err, isResult, ok, result } from '@fischi20/errval' const result = ok(1) const errResult = err('error') if(isResult(object)){ // do something with the result here } const genericResult = result({value: 1})