# nice-try

> Tries to execute a function and discards any error that occurs

Latest version **4.0.2** (published 2025-08-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install nice-try
pnpm add nice-try
yarn add nice-try
bun add nice-try
```

## Health

**Score 48/100 (D)** — status: stable.

Positive: has types package; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 4.0.2 |
| Published | 2025-08-16 |
| First published | 2016-07-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/nice-try) |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 69 |
| Maintainers | electerious |
| Keywords | try, catch, error |

## Links

- npm: https://www.npmjs.com/package/nice-try
- Repository: https://github.com/electerious/nice-try
- Issues: https://github.com/electerious/nice-try/issues
- npm.io page: https://npm.io/package/nice-try

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 4.0.2 (latest) — 2025-08-16
- 4.0.1 — 2025-07-07
- 4.0.0 — 2025-07-07
- 3.0.1 — 2022-09-17
- 3.0.0 — 2021-02-28
- 2.1.0 — 2020-10-10
- 2.0.1 — 2020-03-20
- 2.0.0 — 2018-10-28
- 1.0.5 — 2018-08-25
- 1.0.4 — 2017-08-08
- 1.0.3 — 2017-06-10
- 1.0.2 — 2016-08-05
- 1.0.1 — 2016-08-05
- 1.0.0 — 2016-07-30

## README

# nice-try

![Build](https://github.com/electerious/nice-try/workflows/Build/badge.svg)

A function that tries to execute a function and discards any error that occurs.

## Install

```bash
npm install nice-try
```

## Usage

```js
import niceTry from 'nice-try'

niceTry(() => JSON.parse('true')) // true
niceTry(() => JSON.parse('error')) // undefined
niceTry() // undefined
niceTry(true) // undefined
```

```js
import niceTry from 'nice-try/promises'

await niceTry(async () => JSON.parse('true')) // true
await niceTry(async () => JSON.parse('error')) // undefined
```

## API

### `nice-try`

#### Parameters

- `fn` `{Function}` The function to execute.

#### Returns

- `{*}` The return value of the function, or `undefined` if an error occurred.

### `nice-try/promises`

#### Parameters

- `fn` `{Function}` An asynchronous function to execute.

#### Returns

- `{Promise<*>}` The result of the function if it resolves successfully, otherwise undefined if an error is thrown.

## What others say

This module is a controversial thing and people like to get mad about it. I decided to give those people a voice. Here's what they're saying.

> fundamentally inane

— frou_dh

> the worst Javascript library […] in several categories

— kuzux

> how is this a thing

— uint8_t

> This is blowing my mind

— mr_jim_lahey

> a shit-tastic try-catch alias

— Dropping_fruits

> I assumed every commit was a joke, but after researching the maintainer a bit I'm not sure it is...

— MrPineappleHat

---
_Source: https://npm.io/package/nice-try · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
