Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
satire-foxfire opened this issue Oct 9, 2024 · 0 comments
Open

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

satire-foxfire opened this issue Oct 9, 2024 · 0 comments
Labels
doc Issues and PRs related to the documentations. util Issues and PRs related to the built-in util module.

Comments

@satire-foxfire
Copy link

satire-foxfire commented Oct 9, 2024

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.

@satire-foxfire satire-foxfire added the doc Issues and PRs related to the documentations. label Oct 9, 2024
@RedYetiDev RedYetiDev added the util Issues and PRs related to the built-in util module. label Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

2 participants