Skip to content

Instantly share code, notes, and snippets.

@bmax
Created February 17, 2021 04:35
Show Gist options
  • Save bmax/977433a31e1a9a067b44fd6d9ef432ad to your computer and use it in GitHub Desktop.
Save bmax/977433a31e1a9a067b44fd6d9ef432ad to your computer and use it in GitHub Desktop.
const async_hooks = require('async_hooks')
const fs = require('fs')
// Create a new AsyncHook instance. All of these callbacks are optional.
const asyncHook = async_hooks.createHook({
init,
before,
after,
destroy,
promiseResolve,
})
asyncHook.enable()
setTimeout(() => {
console.log('test1')
}, 1000)
asyncHook.disable()
function init(asyncId, type, triggerAsyncId, resource) {
const eid = async_hooks.executionAsyncId()
fs.writeSync(
1,
`${type}(${asyncId}): trigger: ${triggerAsyncId} execution: ${eid}\n`
)
}
function before(asyncId) {
fs.writeSync(1, `before ${asyncId}`)
}
function after(asyncId) {
fs.writeSync(1, `after ${asyncId}`)
}
function destroy(asyncId) {
fs.writeSync(1, `destroy ${asyncId}`)
}
function promiseResolve(asyncId) {
fs.writeSync(1, `promise ${asyncId}`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment