Skip to solution
hardFrontend

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

457 views
01

Understand 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.

decoratorsusingresource
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Hard

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The using-only portion below was actually run and verified; the decorator-combined portio

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal, fully verified: using's disposal mechanism alone, genuinely runnable and correct — the exact mechanism a class decorator would automate
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 149 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track