Skip to content

Instantly share code, notes, and snippets.

@mafintosh
Created August 30, 2018 20:43
Show Gist options
  • Save mafintosh/0d3008428d830d6ce833910d4d1a5e1d to your computer and use it in GitHub Desktop.
Save mafintosh/0d3008428d830d6ce833910d4d1a5e1d to your computer and use it in GitHub Desktop.
const hooks = require('async_hooks')
const promises = new Set()
const hook = hooks.createHook({
init (asyncId, type) {
if (type === 'PROMISE') {
promises.add(asyncId)
}
},
after (asyncId) {
promises.delete(asyncId)
},
destroy (asyncId) {
promises.delete(asyncId)
}
})
hook.enable()
function wait () {
return new Promise(function (resolve) {
setTimeout(resolve, 100)
})
}
async function foo () {
await wait()
await wait()
}
foo()
setTimeout(function () {
hook.disable()
console.log('promises.size', promises.size)
}, 500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment