adam-stover / try-nice

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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, tryNiceAsync } from 'try-nice'
const [result] = tryNice(() => 1)
//result === 1

const getTwo = async () => 2
const [asyncResult] = await tryNiceAsync(getTwo)
// asyncResult === 2

const getValue = async (value) => value
const [parameterizedResult] = await tryNiceAsync(getValue, 3)
// parameterizedResult === 3

const getError = async () => {
  throw new Error()
}

const [emptyResult, error] = await tryNiceAsync(getError)
// emptyResult === undefined
// error instanceof Error

Usage Typescript

import { tryNice, tryNiceAsync } from 'try-nice'
const [result] = tryNice(() => 1)
//result === 1

const getTwo = async (): number => 2
const [asyncResult] = await tryNiceAsync<number>(getTwo)
// asyncResult === 2

const getValue = async (value: string): string  => value
const [parameterizedResult] = await tryNiceAsync<string>(getValue, 3)
// parameterizedResult === 3

const getError = async (): void => {
  throw new Error()
}

const [emptyResult, error] = await tryNiceAsync<any, Error>(getError)
// emptyResult === undefined
// error instanceof Error

Contributors

Name
An Duong

License

MIT © An Duong

About

License:MIT License


Languages

Language:TypeScript 59.6%Language:JavaScript 40.4%