Skip to content

async_hooks Use Case #46232

Open
Open
@anywhichway

Description

@anywhichway

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;
    };

Metadata

Metadata

Assignees

No one assigned

    Labels

    async_hooksIssues and PRs related to the async hooks subsystem.docIssues and PRs related to the documentations.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions