當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。