triggerAsyncId
triggerAsyncId 是导致(或“触发”)新资源初始化并导致 init 调用的资源的 asyncId。
下面是 triggerAsyncId 的简单演示:
async_hooks.createHook({
init(asyncId, type, triggerAsyncId) {
const eid = async_hooks.executionAsyncId();
fs.writeSync(
process.stdout.fd,
`${type}(${asyncId}): trigger: ${triggerAsyncId} execution: ${eid}\n`);
}
}).enable();
require('net').createServer((conn) => {}).listen(8080);当使用 nc localhost 8080 访问服务器时的输出:
TCPSERVERWRAP(5): trigger: 1 execution: 1
TCPWRAP(7): trigger: 5 execution: 0TCPSERVERWRAP 是接收连接的服务器。
TCPWRAP 是来自客户端的新连接。
当建立新连接时,则立即构造 TCPWrap 实例。
这发生在任何 JavaScript 堆栈之外。
(0 的 executionAsyncId() 表示其是从 C++ 执行的,上面没有 JavaScript 堆栈。)只有这些信息,就不可能将资源链接在一起,因为它们是什么导致它们被创建,所以 triggerAsyncId 被赋予传播什么资源对新资源的存在负责的任务。