高级前端node【Q316】node 中如何查看函数异步调用栈

node 中如何查看函数异步调用栈

Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 318

Author 回答者: z54749412

const async_hooks = require(‘async_hooks’);

// 创建一个 AsyncHooks 实例 const asyncHook = async_hooks.createHook({ init, destroy });

// 初始化回调函数 function init(asyncId, type, triggerAsyncId, resource) { const currentStack = new Error().stack; console.log(Init asyncId: ${asyncId}, type: ${type}, stack: ${currentStack}); }

// 销毁回调函数 function destroy(asyncId) { console.log(Destroy asyncId: ${asyncId}); }

// 启用 AsyncHooks 实例 asyncHook.enable();