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 🙏

© 2026 – Pkg Stats / Ryan Hefner

promise-police

v0.1.7

Published

Warn the developer if a promise has been left unhandled

Readme

promise-police

In alpha. Please see the Caveats.

What it does

promise-police is a debugging tool that detects promises in your application which you have forgotten to handle.

This is especially useful for developers who are just starting to learn promises. It helps to catch common mistakes such as forgetting to return or await a promise.

It enforces "The Golden Rule of Promises":

Every promise must either be awaited, or returned, or caught.

How to use it

Basic usage, only in development:

if (!process.env.NODE_ENV) {
  require('promise-police')
}

We only load the module in development mode, so our production app can create lots of promises with no additional overhead.

See it in action

If you write code like this, nothing will change:

createPromise().then(handleSuccess).catch(handleError)

createPromise().then(handleSuccess, handleError)

createPromise().catch(handleError)

await createPromise()

return createPromise()

But code like this will alert the promise-police:

// You forgot to catch
createPromise().then(handleSuccess)

// You forgot to do anything with the promise
createPromise()

Specifically, if you create a promise and you don't add any handlers within the timeout period, a warning will be logged:

Unhandled promise detected by promise-police: Error: Promise has not been .then()-ed or .catch()-ed!
    at createPromise (test.js:24:11)
    at main (test.js:12:4)
    at ...

By looking down the stacktrace, you should be able to find the line of code where you forgot to handle the promise.

Note that in the case of a promise chain, the stacktrace will only indicate the last .then() in the chain, not the start of the chain. For more informative stacktraces, you may want to enable long stacktrace support: node, ES6 Promise, bluebird, Q

How it works

By default, this module replaces the global Promise constructor with our own constructor which always calls the original Promise constructor, but also adds some magic to the newly created promise.

Configuration

Instead of the basic uage above, you can configure the module to behave differently.

In environments where the logged stacktrace is not helpful (such as Expo with CRNA), you can throw an error instead of logging:

require('promise-police/configure')({ throwError: true });

This will produce more useful output, because a thrown error will have its stack passed through the source map.

Advanced configuration

Or you can perform a fully detailed configuration:

const promiseWrapper = require('promise-police/promiseWrapper')

promiseWrapper.wrap(global, 'Promise', {
  // How long to wait for a .then() or .catch() to be added to the promise.
  //
  //timeout: 2000,

  // Some libraries do not always handle the promises they create.
  // If you receive warnings that you want to mute, you can add a regexp that will match only that code's stacktrace.
  //
  ignoreList: promiseWrapper.defaultOptions.ignoreList.concat([
    / at Mongoose.connect /,
    / at Mongoose.model /
  ]),

  // If set to false, only the first promise created is checked.  Promises resulting from later .then()s are not checked.
  //
  //checkChains: true,

  // Set this true to consider a chain has been sufficiently handled after `.then(good, bad)`
  // Set this false to consider a chain only sufficiently handled after `.then(good).catch(bad)`
  // (Probably not a good idea to set it false: Many libraries in the wild use the former approach.)
  //
  //twoFunctionsCompleteChain: true,

  // Instead of logging, throw an error when an unhandled promise is detected
  // This is good when using create-react-native-app, because the stack will display properly
  //
  //throwError: false
})

You could also potentially add this behaviour to other promise libraries (e.g. bluebird or Q), but that feature is not yet well supported.

Caveats

  • A lot of libraries do not follow the Golden Rule: they skip doing .catch() because they are certain that the promise will resolve. promise-police will detect and report these violations until a suitable regexp is added to the ignoreList. This is a pain! We only really want to catch violations of the Golden Rule within our app.

    We could add /node_modules/ as an ignore regexp. That would work, but it would also avoid checking many promises that we want to check: promises created inside a package but returned to the app for handling, or promises created inside app code but on a stack that was initiated from inside a package.

  • If you are using mongoose, and you create a query but forget to handle it, promise-police will not detect it. That's because mongoose queries do not turn into promises until you either .exec() or .then() them.

  • When using React Native / Expo, the out-of-the-box behaviour is not helpful. Violations are logged, but the lines in the stacktrace do not correspond to the original source code. We probably need to use source maps for this. However, you can get useful stacktraces by throwing an error instead of logging.

Todo

  • Add tests.
  • Allow event handler to be registered instead of throwing an error or logging a warning?
  • Provide the same functionality for Q and Bluebird users? (Refactor so we can do Q = wrap(require('q')).)
  • Test more environments. (React Native, WebPack, ...)
  • Police more constraints, such as those in Soares's post? (I haven't experienced a need for most of them, but YMMV!) One other constraint I would like: Complain if Promises are rejected without an Error object.
  • We could make the regexp blacklist checking more efficient, but that is probably overkill for a simple development tool.

See also

  • Inspiration for this utility came from reading this post about promise debugging by Soares Chen.