try-nice
v4.0.4
Published
try/catch wrapper
Downloads
7
Readme
try-nice
Clean try/catch wrapper
Table of Contents
Install
npm:
npm install try-nice
yarn:
yarn add try-nice
Usage
var { tryNice } = require('try-nice')
function getOne() {
return 1
}
var [result] = tryNice(getOne)
// result === 1
Usage ES6
import { tryNice } from 'try-nice'
const [result] = tryNice(() => 1)
//result === 1
const getTwo = async () => 2
const [asyncResult] = await tryNice(getTwo)
// asyncResult === 2
const getValue = async (value) => value
const [parameterizedResult] = await tryNice(getValue, 3)
// parameterizedResult === 3
const getError = async () => {
throw new Error()
}
const [emptyResult, error] = await tryNice(getError)
// emptyResult === undefined
// error instanceof Error
Usage Typescript
import { tryNice } from 'try-nice'
const [result] = tryNice(() => 1)
//result === 1
const getTwo = async (): number => 2
const [asyncResult] = await tryNice(getTwo)
// asyncResult === 2
const getValue = async (value: string): string => value
const [parameterizedResult] = await tryNice(getValue, 3)
// parameterizedResult === 3
const getError = async (): void => {
throw new Error()
}
const [emptyResult, error] = await tryNice(getError)
// emptyResult === undefined
// error instanceof Error
Contributors
| Name | | ------------ | | An Duong |
License
MIT © An Duong