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

promise-origin-stack

v0.2.0

Published

Lightweight library to add the original stack trace to unhandled errors in promises

Downloads

8

Readme

Promise Origin Stack

Since the dawn of time, there has been one major issue with the use of promises: The stack trace only goes back as far as the Promise's creation (or the last then in most cases).

This leads to long nights spent debuging code.

I know my API call failed, but which of my many modules that use my API wrapper made initial call that failed?

Promise libraries like Q and Bluebird have long had settings to provide "long stack support", but now that native promises are supported by so many browsers and Node, why should we need to use a heavy library just to add this one needed feature?

Now we don't have to. Simply install and register promise-origin-stack and you'll get stack traces that point to the creation of the promise. No other dependencies. Lightweight. Easy to use. What more could you ask for?

Installation

Install it:

npm install --save promise-origin-stack

Usage

Then register it:

require('promise-origin-stack').register();

That's it. Any error thrown by a promise will automatically have an additional stack trace from the creation of the promise appended to it's own stack trace.

You can continue to use your promises with no modification to the code.

Caveats

When is the Promise created?

The originating stack will point to the creation of the promise that threw the error. Since each .then returns a new promise, the origin point will usually point to the .then before the erroring function, not necessarily Promise.resolve or new Promise.

Performance

In order to get the original stack trace, promise-origin-stack has to create an error and save the stack whenever a promise is created. Unfortunately, creating an error is not a cheap operation. It takes a couple of milliseconds. Granted, that doesn't sound like a lot, but if you are creating many promises, chained together, this can take some time. Usually, this is only noticeable in limited use cases.

To mitigate this problem, there is a flag to allow us to temporarily (or permanently) disable origin stacks after registering this package. Simply add the following code before you create your Promise.

Promise.disableOriginStacks = true;

Example

require('promise-origin-stack').register();

function bad() {
   throw new Error('This error will have extra information');
}

function appendSomething(s) {
   return s + ' something';
}

(function() {
   Promise.resolve('new promise')
      .then(appendSomething)
      .then(appendSomething)
      .then(appendSomething) // This is where the stack trace will point [1]
      .then(bad) // This is where the error is thrown [2]
      .then(appendSomething)
      .then(appendSomething);
})();

Will provide stack trace like this:

(node:91797) UnhandledPromiseRejectionWarning: Error: This error will have extra information
    at bad ({Path to File}/example.js:4:10)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
    at Function.Module.runMain (module.js:695:11)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3
Error: Promise Created At:
    at Promise.then (<anonymous>)
    at {Path to File}/example.js:15:8 <<<< Points to the line marked [1] above
    at Object.<anonymous> ({Path to File}/example.js:19:3)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)