try-nice

try/catch wrapper

Usage no npm install needed!

<script type="module">
  import tryNice from 'https://cdn.skypack.dev/try-nice';
</script>

README

try-nice

build status styled with prettier license npm downloads

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