async_hooks.triggerAsyncId()
- 返回: <number> 负责调用当前正在执行的回调的资源 ID。
const server = net.createServer((conn) => {
// 导致(或触发)此回调被调用的资源是新连接的资源。
// 因此 triggerAsyncId() 的返回值是 "conn" 的 asyncId。
async_hooks.triggerAsyncId();
}).listen(port, () => {
// 即使传给 .listen() 的所有回调都包含在 nextTick() 中,
// 但回调本身仍然存在,因为对服务器的 .listen() 进行了调用。
// 所以返回值将是服务器的 ID。
async_hooks.triggerAsyncId();
});
默认情况下,promise 上下文可能无法获得有效的 triggerAsyncId
。
请参阅 promise 执行跟踪部分。
- Returns: <number> The ID of the resource responsible for calling the callback that is currently being executed.
const server = net.createServer((conn) => {
// The resource that caused (or triggered) this callback to be called
// was that of the new connection. Thus the return value of triggerAsyncId()
// is the asyncId of "conn".
async_hooks.triggerAsyncId();
}).listen(port, () => {
// Even though all callbacks passed to .listen() are wrapped in a nextTick()
// the callback itself exists because the call to the server's .listen()
// was made. So the return value would be the ID of the server.
async_hooks.triggerAsyncId();
});
Promise contexts may not get valid triggerAsyncId
s by default. See
the section on promise execution tracking.