# try-to-catch

> function try-catch wrapper for promises

Latest version **4.1.0** (published 2026-07-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install try-to-catch
pnpm add try-to-catch
yarn add try-to-catch
bun add try-to-catch
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.1.0 |
| Published | 2026-07-11 |
| First published | 2018-02-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=22 |
| Dependencies | 0 |
| Unpacked size | 6.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 40 |
| Author | coderaiser |
| Maintainers | coderaiser |
| Keywords | try, catch, function, promise, async, await, try-catch, then |

## Links

- npm: https://www.npmjs.com/package/try-to-catch
- Repository: https://github.com/coderaiser/try-to-catch
- Homepage: http://github.com/coderaiser/try-to-catch
- Issues: https://github.com/coderaiser/try-to-catch/issues
- npm.io page: https://npm.io/package/try-to-catch

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 4.1.0 (latest) — 2026-07-11
- 4.0.5 — 2026-01-31
- 4.0.4 — 2026-01-28
- 4.0.3 — 2026-01-04
- 4.0.2 — 2025-12-31
- 4.0.1 — 2025-12-31
- 4.0.0 — 2025-12-31
- 3.0.1 — 2022-03-09
- 3.0.0 — 2020-02-24
- 2.0.1 — 2019-12-30
- 2.0.0 — 2019-10-16
- 1.1.1 — 2018-11-08
- 1.1.0 — 2018-11-08
- 1.0.2 — 2018-02-13
- 1.0.1 — 2018-02-12
- … 1 more at https://npm.io/package/try-to-catch/versions

## README

# Try to Catch [![NPM version][NPMIMGURL]][NPMURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] [![Coverage Status][CoverageIMGURL]][CoverageURL]

[NPMIMGURL]: https://img.shields.io/npm/v/try-to-catch.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/try-to-catch "npm"
[CoverageURL]: https://coveralls.io/github/coderaiser/try-to-catch?branch=master
[CoverageIMGURL]: https://coveralls.io/repos/coderaiser/try-to-catch/badge.svg?branch=master&service=github
[BuildStatusURL]: https://github.com/coderaiser/try-to-catch/actions?query=workflow%3A%22Node+CI%22 "Build Status"
[BuildStatusIMGURL]: https://github.com/coderaiser/try-to-catch/workflows/Node%20CI/badge.svg

Functional `try-catch` wrapper for `promises`.

## Install

```
npm i try-to-catch
```

## API

### tryToCatch(fn, [...args])

Wrap function to avoid `try-catch` block, resolves `[error, result]`;

### Example

Simplest example with `async-await`:

```js
import {tryToCatch} from 'try-to-catch';

const reject = Promise.reject.bind(Promise);
await tryToCatch(reject, 'hi');
// returns
// [ Error: hi]
```

Can be used with functions:

```js
import {tryToCatch} from 'try-to-catch';

await tryToCatch(() => 5);
// returns
[null, 5];
```

Advanced example:

```js
import {readFile, readdir} from 'node:fs/promises';
import {tryToCatch} from 'try-to-catch';

const [error, data] = await tryToCatch(read, process.argv[2]);

if (error) {
    console.error(error);
    process.exit(1);
}

console.log(data);

async function read(path) {
    const [error, data] = await tryToCatch(readFile, path, 'utf8');
    
    if (!error)
        return data;
    
    if (error.code !== 'EISDIR')
        return error;
    
    return await readdir(path);
}
```

## Related

- [try-catch](https://github.com/coderaiser/try-catch "try-catch") - functional try-catch wrapper.

## License

MIT

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