Open
Description
Affected URL(s)
https://nodejs.org/api/async_hooks.html#async-hooks
Description of the problem
I am not sure how I would accomplish the tracking of un-resolved async functions in this testing tool https://github.com/anywhichway/benchtest, without async_hooks
in any kind of simple manner. The Node docs request the submission of a use case for async_hooks
since the API is not recommended.
Currently the tool only requires a few lines of code:
const asyncTracker = new Map(),
asyncHook = async_hooks.createHook({
init: (asyncId, type, triggerAsyncId, resource) => {
asyncTracker.set(asyncId,{type,triggerAsyncId,resource})
},
destroy: asyncId => {
asyncTracker.delete(asyncId);
},
promiseResolve: asyncId => {
asyncTracker.delete(asyncId);
},
}),
trackAsync = (on) => {
if(on) {
if(!asyncTracker.enabled) {
asyncTracker.clear();
asyncHook.enable();
}
} else if(!on && asyncTracker.enabled){
asyncHook.disable();
}
asyncTracker.enabled = on;
};