2.2.2 • Published 1 year ago
@kreisler/try-catch v2.2.2
Try Catch
A simple try-catch wrapper for functions and async functions.
Usage
Install package
npm i @kreisler/try-catchImport 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');
})();