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

1.0.0 • 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');
})();

Package Sidebar

Install

npm i @kreisler/try-catch

Weekly Downloads

3

Version

1.0.0

License

MIT

Unpacked Size

7.89 kB

Total Files

6

Last publish

Collaborators

  • kreisler