Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
'use strict';
const run = function (getStack) {
process.nextTick(() => {
process.nextTick(() => {
process.nextTick(() => {
process.nextTick(() => {
process.nextTick(() => {
process.nextTick(() => {
console.log(getStack());
})
})
})
})
})
})
}
const AH = require('async_hooks');
const getFrames = function () {
let res = [];
const f0 = Error.prepareStackTrace;
Error.prepareStackTrace = function (_, x) {
res = x;
}
new Error().stack;
Error.prepareStackTrace = f0;
return res;
};
const hook = AH.createHook({
init(asyncId, type, triggerAsyncId, resource) {
const parent = AH.executionAsyncResource();
const frames = getFrames();
if (parent.frames) {
frames.push(...parent.frames);
}
resource.frames = frames;
}
})
.enable();
const getStrackTrace = function () {
return AH.executionAsyncResource().frames.map((x) => x.getFileName() + ':' + x.getLineNumber()).join('\n');
}
run(getStrackTrace);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment