Code Monkey home page Code Monkey logo

long-promise2's Introduction

long-promise2

Long stack traces with Promises, using the brand-new async_hooks API provided by Node 8.0.0.

const lp = require('long-promise2');

lp.enable();

// Simulate some stuff
const stuff = x => 2 + x;

// Simulate a delay.
const delay = ms => x => {
    // This function is `buggy` and will throw if the delay is 100ms.
    if (ms === 100) throw new Error('boom');

    return new Promise(res => {
        setTimeout(() => {
            res(x);
        }, ms);
    });
};

const nestedProblems = x => () => {
    // After the third call, go to an eventual `delay(100)` that throws
    if (x === 3) return Promise.resolve(1).then(delay(100));

    // Promise chain with recursion
    return Promise.resolve(1)
        .then(delay(10))
        .then(stuff)
        .then(nestedProblems(x + 1));
};

// Main program
function run() {
    const p = Promise.resolve(2);

    // Do some things. Something along this promise chain will throw.
    p.then(stuff)
        .then(delay(10))
        .then(stuff)
        .then(delay(10))
        .then(stuff)
        .then(nestedProblems(1))
        .then(stuff)
        .then(stuff)
        .catch(err => {
            console.log(lp.getLongStack(err));
            // Profit
        });
}

run();

This produces the following stack trace:

Error: boom
    at x (/Users/mvaldes/Sandbox/long-promise2/example.js:12:27)
    at <anonymous>
    at Promise.then (<anonymous>)
    at /Users/mvaldes/Sandbox/long-promise2/example.js:23:44
    at <anonymous>
    at Promise.then (<anonymous>)
    at /Users/mvaldes/Sandbox/long-promise2/example.js:29:10
    at <anonymous>
    at Promise.then (<anonymous>)
    at /Users/mvaldes/Sandbox/long-promise2/example.js:29:10
    at <anonymous>
    at Promise.then (<anonymous>)
    at run (/Users/mvaldes/Sandbox/long-promise2/example.js:42:10)
    at Object.<anonymous> (/Users/mvaldes/Sandbox/long-promise2/example.js:51:1)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)

Without long-promise2 enabled, the original stack trace is the following:

Error: boom
    at x (/Users/mvaldes/Sandbox/long-promise2/example.js:12:27)
    at <anonymous>

This is very early work, any contributions are welcome.

install

with npm do:

npm install long-promise2

license

MIT

long-promise2's People

Contributors

mvaldesdeleon avatar

Stargazers

Pelle Wessman avatar

Watchers

 avatar

Forkers

rafaelhrtd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.