If your codebase uses the two methods .setImmediate
and .nextTick
from the library async,
then after doing the following monkey-patching you can call the function global.printStack()
to get the full stack trace.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var a = require('async');
global.previousStack = [];
var s = a.setImmediate;
a.setImmediate = function(fn) {
var stack = global.previousStack.slice();
stack.push(new Error().stack);
s(function() {
global.previousStack = stack;
fn();
});
};
var n = a.nextTick;
a.nextTick = function(fn) {
var stack = global.previousStack.slice();
stack.push(new Error().stack);
n(function() {
global.previousStack = stack;
fn();
});
};
global.printStack = function() {
global.previousStack.forEach(function(stack) {
console.log(stack);
});
console.log(new Error().stack);
};
This hack is in the earliest stage - it needs development to avoid using global
and also parsing and joining of the stacks to remove the repeated lines.
By the way, it is fun to know the difference between .setImmediate
and .nextTick
.