当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Node.js AsyncHook async_hooks.triggerAsyncId()用法及代码示例

async_hooks.triggerAsyncId()

  • 返回: <number> 负责调用当前正在执行的回调的资源的 ID。
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 上下文可能无法获得有效的 triggerAsyncId 。请参阅 promise execution tracking 部分。

相关用法


注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 async_hooks.triggerAsyncId()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。