Skip to content

util: add util.disposer helper to wrap a dispose function #58585

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

legendecas
Copy link
Member

Add util.disposer and util.asyncDisposer to conveniently wrap a
function to be a disposable, and allow it to be used with using
declaration.

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module. labels Jun 5, 2025
@Renegade334
Copy link
Contributor

Is this not the same functionality that DisposableStack.prototype.defer already provides natively?

Add `util.disposer` and `util.asyncDisposer` to conveniently wrap a
function to be a disposable, and allow it to be used with `using`
declaration.
@legendecas
Copy link
Member Author

Is this not the same functionality that DisposableStack.prototype.defer already provides natively?

It is. This is a helper function if you just want to wrap a function, without the need to create a DisposableStack/AsyncDisposableStack, or define an object with Symbol.dispose/Symbol.asyncDispose.

Copy link

codecov bot commented Jun 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.20%. Comparing base (7ffa029) to head (c1f1080).
Report is 41 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #58585      +/-   ##
==========================================
- Coverage   90.24%   90.20%   -0.05%     
==========================================
  Files         635      636       +1     
  Lines      187633   187580      -53     
  Branches    36853    36852       -1     
==========================================
- Hits       169336   169213     -123     
- Misses      11082    11153      +71     
+ Partials     7215     7214       -1     
Files with missing lines Coverage Δ
lib/internal/util/disposer.js 100.00% <100.00%> (ø)
lib/util.js 97.79% <100.00%> (+0.02%) ⬆️

... and 64 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.


```mjs
{
await using _ = util.disposer(async function disposer() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await using _ = util.disposer(async function disposer() {
await using _ = util.asyncDisposer(async function disposer() {

defineLazyProperties(
module.exports,
'internal/util/disposer',
['disposer', 'asyncDisposer', 'Disposer', 'AsyncDisposer'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disposer and AsyncDisposer are not actually exported?

with `using` declaration.

If an error is thrown in the function, instead of returning a promise,
the error will be wrapped in a rejected promise.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that DisposableStack.defer(...) exists I'm not super convinced that this is needed or all that useful.

{
  using ds = new DisposableStack();
  ds.defer(onDispose);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants