Skip to the solution
HardFrontend6 min read

How do you combine Stage 3 decorators with using for auto-disposable resources?

457 views

The problem

Question presented to candidate: "If you have a class representing a database connection, how would you combine a decorator with using/await using so that every instance is automatically, correctly disposable β€” without manually writing [Symbol.dispose] on every single class yourself?"

What a strong answer should cover:

  • πŸ“Œ Interview term: the real, combined pattern β€” a class decorator can programmatically ADD a [Symbol.dispose]/[Symbol.asyncDispose] method to every class it's applied to, so any instance of that class automatically, correctly works with using/await using (covered in more depth in this bank's own dedicated Explicit Resource Management question) β€” without hand-writing the disposal method on each individual class.
  • πŸ“Œ Interview term: the real, direct honesty check β€” verified directly, in both Node v24 and a real, current browser: the DECORATOR half of this pattern genuinely cannot run natively anywhere today (a real SyntaxError, matching this bank's own dedicated Decorators question) β€” only the using half is genuinely, directly runnable.
  • πŸ“Œ Interview term: using on its own, fully verified β€” verified directly: a resource implementing [Symbol.dispose] (written by hand, without a decorator) is genuinely, automatically disposed when a using-declared block exits, in strict reverse declaration order for multiple resources β€” the real mechanism the decorator half would ultimately be automating.
  • A precise answer names the real, accurate combined syntax (a class decorator using context.addInitializer to attach the dispose method to each new instance) while being explicit that it is illustrative Stage 3 syntax, not something that can be pasted and run today.
  • A precise answer names the real, practical motivation: without this combination, every class needing disposal (a DB connection, a file handle, a lock) must hand-write its own [Symbol.dispose]; a shared @disposable-style decorator would let that boilerplate be written ONCE and reused across many classes.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; honestly separating what IS directly verifiable (using alone) from what ISN'T (the decorator half) is the strong signal.

Code / implementation expected: Yes for the using-only portion (genuinely runnable, verified directly); the combined decorator+using syntax is shown as real, accurate reference code, honestly marked non-runnable.

  • decorators
  • using
  • resource

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, fully verified: using's disposal mechanism alone, genuinely runnable and correct β€” the exact mechanism a class decorator would automate

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 149 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

How does the Temporal API fix JavaScript Date β€” Instant, PlainDate and ZonedDateTime?

Continue