https://github.com/mvaldesdeleon/long-promise
Long stack traces for Promises (Node 8 only).
https://github.com/mvaldesdeleon/long-promise
async-hooks node promise stack stacktrace
Last synced: about 1 year ago
JSON representation
Long stack traces for Promises (Node 8 only).
- Host: GitHub
- URL: https://github.com/mvaldesdeleon/long-promise
- Owner: mvaldesdeleon
- License: other
- Created: 2017-06-01T19:23:51.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-02T08:53:41.000Z (about 9 years ago)
- Last Synced: 2025-04-23T06:46:47.014Z (about 1 year ago)
- Topics: async-hooks, node, promise, stack, stacktrace
- Language: JavaScript
- Size: 13.7 KB
- Stars: 12
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# long-promise
Long stack traces with Promises, using the brand-new `async_hooks` API provided by Node 8.0.0.
```JS
const lp = require('long-promise');
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-promise/example.js:12:27)
at
at Promise.then ()
at /Users/mvaldes/Sandbox/long-promise/example.js:23:44
at
at Promise.then ()
at /Users/mvaldes/Sandbox/long-promise/example.js:29:10
at
at Promise.then ()
at /Users/mvaldes/Sandbox/long-promise/example.js:29:10
at
at Promise.then ()
at run (/Users/mvaldes/Sandbox/long-promise/example.js:42:10)
at Object. (/Users/mvaldes/Sandbox/long-promise/example.js:51:1)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
```
Without `long-promise` enabled, the original stack trace is the following:
```
Error: boom
at x (/Users/mvaldes/Sandbox/long-promise/example.js:12:27)
at
```
This is very early work, any contributions are welcome.
# install
with [npm](https://npmjs.org) do:
```
npm install long-promise
```
# license
MIT