Skip to content

clarification on when/how to use util.aborted #55340

Closed
@satire-foxfire

Description

@satire-foxfire

Affected URL(s)

https://nodejs.org/api/util.html#utilabortedsignal-resource

Description of the problem

The docs aren't entirely clear on the purpose of the second optional argument (resource), how it's supposed to be used, or when it's useful. One of the primary use cases for this util function seems to be aborting a promise that doesn't support abort natively, but I'm not sure what I should pass as the resource?

import { aborted } from 'node:util';

const ac = new AbortController();
const client = new SomeRandomDatabaseClient();

const results = await Promise.race([
  client.uncancelableQuery(),
  aborted(ac.signal, /* what am i supposed to pass here */),
]);

// somewhere else ac.abort() may or may not ever be called
// but assume ac does go out of scope and get gc'd eventually

Am I thinking about this use case correctly, or is not passing resource somehow creating a memory leak / preventing things from being cleaned up properly?

EDIT: It seems I misunderstood the second argument being optional, it appears required, but questions still remain how it's supposed to be used for the example above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions