const async_hooks = require("async_hooks")

let callTrace = []

async_hooks.createHook({
  init: (asyncId, type, triggerAsyncId, resource) => {
    callTrace.push({ resource, asyncId, triggerAsyncId })
  },
  before(asyncId) {

  },
  after(asyncId) {

  },
  destroy(asyncId) {
    // console.log(`destroy asyncId:${asyncId}`);
  }
}).enable()

async function test() {

  const aa = await Promise.resolve(1);
  const bb = await test2()
}

async function test2() {
  console.log(callTrace)
  return 1
}

test()

我们来分析一下这段代码，asyncId分别对应的是那个异步操作