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

throw-rejects

v1.0.0

Published

Throw unhandled Promise rejections

Downloads

28

Readme

throw-rejects Build status for Throw Rejects

Throw unhandled Promise rejections

Fixes error handling for Promises, including the swallowed error problem, by converting unhandled rejections into normal thrown exceptions. Ensures that the process crashes correctly, just like throwing non-Promise errors does.

Tip: Be a good citizen and only use this in top level packages like apps and CLIs, as opposed to libraries, to avoid surprising any dependents.

Why?

The main argument against crashing for uhandled rejections is that JavaScript allows programs to catch rejections asynchronously and crashing as soon as a Promise is rejected prevents this behavior. However, adding those "late" .catch() handlers asynchronously is almost always a bad idea and programs can be designed to avoid doing so. Crashing with a stack trace, as this module does, is a much more safe and helpful default behavior, as opposed to doing nothing and hoping that maybe someone will handle the error in the future.

You can still use .catch() to avoid crashing, see usage for details.

Install

npm install throw-rejects --save

Usage

Get it into your program.

const throwRejects = require('throw-rejects');

Activate the listener, which will convert unhandled Promise rejections into thrown exceptions. We recommend doing this as early as possible, in case your other dependencies use Promises.

throwRejects();

Or, you can shorten this by calling the function immediately.

require('throw-rejects')();

Alternatively, use the register script, which self-activates as a side effect.

require('throw-rejects/register');

The register script is especially useful for modules using import, because that syntax does not allow immediate function calls. Thus, importing register is the shortest syntax in that environment.

import 'throw-rejects/register';

Handling errors

After the listener is activated, you can still handle errors yourself to avoid crashing, as long as you don't do something weird like reject a Promise and then start a timeout which adds the .catch() 5 seconds later, for example.

// Will crash with throw-rejects.
// Will NOT crash without throw-rejects.
const prom = Promise.reject(new Error('Uh oh'));
const prom = Promise.reject(new Error('Uh oh'));
// Prevents throw-rejects from crashing the process.
// Will NOT crash, with or without throw-rejects.
prom.catch(() => {});
const prom = Promise.reject(new Error('Uh oh'));
setTimeout(() => {
    // Will crash with throw-rejects. Does not prevent a crash because it is too late.
    // Will NOT crash without throw-rejects.
    prom.catch(() => {});
}, 5);

Specifically, rejections will be considered handled and the process will not crash as long as you add .catch() anytime within the same event loop as the Promise is rejected, That is basically anywhere you would normally do so. It doesn't have to be chained directly, it can be later in a function, etc.

API

throwRejects()

Activates a listener for unhandledRejection events that throws the associated error as an exception. This will, in turn, bubble up to uncaughtException and, by default, crash the process and print a stack trace.

Contributing

See our contributing guidelines for more details.

  1. Fork it.
  2. Make a feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request.

License

MPL-2.0 © Seth Holladay

Go make something, dang it.