2.2.2 • Published 10 months ago

@kreisler/try-catch v2.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

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

10 months ago

2.0.1

10 months ago

2.0.0

10 months ago

1.0.0

1 year ago