@kreisler/try-catch
TypeScript icon, indicating that this package has built-in type declarations

2.2.2 • Public • Published

Try Catch

A simple try-catch wrapper for functions and async functions.

Usage

Install package

npm i @kreisler/try-catch

Import module

import { tryCatch, tryCatchPromise } from "@kreisler/try-catch";

or

Import commonjs

const { tryCatch, tryCatchPromise } = require("@kreisler/try-catch");

Example #1

const { tryCatch } = require('@kreisler/try-catch');
const { parse } = JSON;
const [error, result] = tryCatch(parse, 'hello');

if (error)
    console.error(error.message);

Example #2

(async () => {
const { readFile, readdir } = require('fs/promises');
const { tryCatchPromise } = require('@kreisler/try-catch');

const [error, data] = await tryCatchPromise(readFile, 'path', 'utf8');

if (!error){
  return data;
}
if (error.code !== 'EISDIR'){
  return error;
}
return await readdir('path');
})();

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.2.270latest

Version History

VersionDownloads (Last 7 Days)Published
2.2.270
2.0.174
2.0.091
1.0.04

Package Sidebar

Install

npm i @kreisler/try-catch

Weekly Downloads

239

Version

2.2.2

License

MIT

Unpacked Size

20.6 kB

Total Files

26

Last publish

Collaborators

  • kreisler