Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

try-to-catch

coderaiser155.4kMIT4.0.3TypeScript support: definitely-typed

function try-catch wrapper for promises

try, catch, function, promise, async, await, try-catch, then

readme

Try to Catch NPM version Build Status Coverage Status

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:

import tryToCatch from 'try-to-catch';

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

Can be used with functions:

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

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

Advanced example:

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);
}

License

MIT

changelog

2026.01.04, v4.0.3

fix:

  • da5a335 try-to-catch: exports

2025.12.31, v4.0.2

fix:

  • 53e1028 try-to-catch: drop support of node < 22
  • 9e77c4f try-to-catch: revert ERR_REQUIRE_CYCLE_MODULE

2025.12.31, v4.0.1

fix:

  • cdfd2b3 try-to-catch: ERR_REQUIRE_CYCLE_MODULE

2025.12.31, v4.0.0

feature:

  • 3a97714 try-to-catch: drop support of node < 20
  • d0914ef try-to-catch: migrate to ESM
  • 9334dd0 try-to-catch: supertape v11.5.1
  • 8c8adee try-to-catch: putout v41.2.3
  • 93096f9 try-to-catch: nodemon v3.1.11
  • 1923ff6 try-to-catch: madrun v11.0.6
  • 3b0c5da try-to-catch: eslint-plugin-putout v29.2.0
  • 0955a6c try-to-catch: eslint v9.39.2
  • bca7769 try-to-catch: c8 v10.1.3
  • be385bd (package) eslint-plugin-putout v15.1.1

2022.03.09, v3.0.1

feature:

  • (package) supertape v7.2.0
  • (package) eslint v8.10.0
  • (package) madrun v9.0.0
  • (package) eslint-plugin-putout v14.4.0
  • (package) putout v25.4.1

2020.02.24, v3.0.0

feature:

  • (try-to-catch) promise -> async

2019.12.30, v2.0.1

fix:

  • (try-to-catch) minimal node: v4 -> v6

feature:

  • (package) putout v7.3.4
  • (package) nyc v15.0.0
  • (package) nodemon v2.0.2

2019.10.16, v2.0.0

feature:

  • (package) putout v6.15.1
  • (package) madrun v3.0.6
  • (try-to-catch) drop support of node < 4
  • (package) nyc v14.1.1
  • (package) eslint v6.1.0

2018.11.08, v1.1.1

fix:

  • (try-to-catch) wraptile -> noArg

2018.11.08, v1.1.0

feature:

  • (try-to-catch) add support of a functions
  • (package) redrun v7.0.2
  • (package) nyc v13.0.1
  • (package) eslint v5.6.0
  • (package) babel v7.0.0
  • (package) redrun v6.0.0

2018.02.13, v1.0.2

fix:

  • (package) legacy

2018.02.12, v1.0.1

feature:

  • (package) keywords: then