以下代码
import async_hooks from 'async_hooks'
console.log(`father: "${async_hooks.triggerAsyncId()}" - current: "${async_hooks.executionAsyncId()}"`)
const asyncHook = async_hooks.createHook({
init(asyncId, type, triggerAsyncId, resource) {
console.log(`creating - father: "${triggerAsyncId}" - current: "${asyncId}"`, {
type,
resource,
})
},
destroy(asyncId) {
console.log(`destroying - "${asyncId}"`)
},
})
asyncHook.enable()
打印以下标准输出:
father: "0" - current: "1"
destroying - "4"
creating - father: "0" - current: "5" {
type: 'TickObject',
resource: {
callback: [Function: afterWriteTick],
args: [ [Object] ],
[Symbol(async_id_symbol)]: 5,
[Symbol(trigger_async_id_symbol)]: 0
}
}
✨ Done in 2.22s.
问题:
- 什么是“4”?
- 什么是“TickObject”以及为什么在 Node.js 退出之前不清理“5”?
- 与
console.log
异步一样,我希望 async_hooks 将永远被调用。为什么我在输出中看不到无限递归?