npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tryon

v1.0.1

Published

Simplifies error control in your JavaScript/TypeScript

Downloads

7

Readme

Overview

Currently, there are basically two options to catch errors in your JavaScript code:

Option 1: try/catch for a) synchronous code or b) code using the async/await keywords:

try {
  // code logic here
} catch (error) {
  // error handling here
}

Option 2: catch handler when using a Promise:

promise
  .then((result) => {
    // code logic here
  })
  .catch((error) => {
    // error handling here
  });

However, these default approaches have some issues:

  • verbosity
  • different options to treat errors (sync x promise)
  • no default error handling

What if we could simplify the error control, with a cleaner usage, and also have a default error handling? This is the goal of the tryon package and here it is how you can use it:

tryon(() => {
  // code logic here
});

The simple example below demonstrates it with a real code. See below for additional examples.

// You just need to wrap your code within a function
tryon(() => {
  // Your code starts here
  function throwAnError() {
    throw new Error("This error should be fired");
  }
  throwAnError();
  console.log("This code should never run");
});

The above code will gracefully run with the error being printed to the console:

This error should be fired

How to install:

npm install tryon

Examples

import tryon, { changeErrorFn } from "tryon";

const newErrorFn = (error) => {
  console.log("It works!!! The error is:", error.message);
};
changeErrorFn(newErrorFn);

tryon(() => {
  function throwAnError() {
    throw new Error("This error should be fired");
  }
  throwAnError();
  console.log("This code should never run");
});
import tryon, { changeErrorFn } from "tryon";

const newErrorFn = (error) => {
  console.log("It works!!! The error is:", error.message);
};
changeErrorFn(newErrorFn);

await tryon(async () => {
  function throwAnError() {
    throw new Error("This error should be fired");
  }

  const p = new Promise((resolve, reject) => {
    throwAnError();
    console.log("This code should never run");
    resolve(true);
  });

  const value = await p; // Promise throws an error
  console.log("This code should never run");
});
import tryon from "tryon";

await tryon(() => {
  function throwAnError() {
    throw new Error("This error should be fired");
  }

  const p = new Promise((resolve, reject) => {
    throwAnError();
    console.log("This code should never run");
    resolve(true);
  });

  // It is necessary to return the promise, to sync with the external await,
  // otherwise the promise will be unsynced and can not be catched.
  return p;
});
import tryon from "tryon";

await tryon(async () => {
  const p = new Promise((resolve, reject) => {
    reject(false);
  });
  await p; // Promise will reject

  console.log("This code should never run");
});
import tryon from "tryon";

tryon(
  () => {
    function throwAnError() {
      throw new Error("This error should be fired");
    }
    throwAnError();
    console.log("This code should never run");
  },
  (error) => {
    console.log("It works!!! The error is:", error.message);
  }
);

🛠 Technologies

The following libraries/frameworks were used on the project:

Author

Done with ❤️ by Marcos Pereira 👋🏽 Contact me!

Linkedin Badge Gmail Badge