Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/anduong96/try-nice

Make try/catch nicer
https://github.com/anduong96/try-nice

Last synced: about 1 month ago
JSON representation

Make try/catch nicer

Awesome Lists containing this project

README

        

# try-nice

[![build status](https://img.shields.io/travis/com/anduong96/try-nice.svg)](https://travis-ci.com/anduong96/try-nice)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![license](https://img.shields.io/github/license/anduong96/try-nice.svg)](LICENSE)
[![npm downloads](https://img.shields.io/npm/dt/try-nice.svg)](https://npm.im/try-nice)

> Clean try/catch wrapper

## Table of Contents

* [Install](#install)
* [Usage](#usage)
* [Contributors](#contributors)
* [License](#license)

## Install

[npm][]:

```sh
npm install try-nice
```

[yarn][]:

```sh
yarn add try-nice
```

## Usage

```js
var { tryNice } = require('try-nice')

function getOne() {
return 1
}

var [result] = tryNice(getOne)
// result === 1
```

## Usage ES6

```js
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
```js
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](LICENSE) © An Duong

##

[npm]: https://www.npmjs.com/

[yarn]: https://yarnpkg.com/