try-to-js
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

try-to-js

A lightweight try/catch wrapper for easy error handling that improves code readability by removing the clutter of traditional try/catch blocks.

Badges

npm npm

Installation

To install the package, run:

npm i try-to-js

Examples

Without using try-to-js

import { tryTo } from "try-to-js";

function withoutUsingTryTo() {
  let res: ReturnType<typeof syncFunctionThatCanThrowError> | undefined;
  try {
    res = syncFunctionThatCanThrowError();
  } catch (err) {
    console.log(err);
    return;
  }
  // do something with res
}

Using try-to-js

import { tryTo } from "try-to-js";

function usingTryTo() {
  const [err, res] = tryTo(syncFunctionThatCanThrowError);
  if (err) {
    console.log(err);
    return;
  }
  // do something with res
}

Notes

  • If no error is thrown, the err value will be undefined, and res will hold the returned value of syncFunctionThatCanThrowError().
  • If an error is thrown, the err value will contain the error that was thrown, and res will be undefined.

License

MIT License

Readme

Keywords

Package Sidebar

Install

npm i try-to-js

Weekly Downloads

19

Version

1.0.4

License

MIT

Unpacked Size

3.78 kB

Total Files

7

Last publish

Collaborators

  • arbz