Skip to the solution
MediumFrontend5 min read

What does using / await using do for Explicit Resource Management (ES2024)?

420 views

The problem

Question presented to candidate: "If you declare using r1 = resource1, then using r2 = resource2 inside a block, and an error is thrown right after β€” do the resources still get cleaned up? And in what order?"

What a strong answer should cover:

  • πŸ“Œ Interview term: using declaration β€” declares a resource that must implement [Symbol.dispose](); the engine genuinely, automatically calls that method when the enclosing block exits β€” normally OR via an early return/break/thrown error.
  • πŸ“Œ Interview term: the real, direct answer to the prompt's first question β€” verified directly: a real error thrown right after declaring a using resource genuinely still triggers disposal β€” the resource's [Symbol.dispose] ran correctly BEFORE the error propagated to a surrounding catch block.
  • πŸ“Œ Interview term: the real, direct answer to the prompt's order question β€” verified directly with 3 real resources: disposal genuinely happens in strict reverse declaration order β€” the LAST-declared resource is disposed FIRST, mirroring a real, standard stack-unwinding discipline.
  • πŸ“Œ Interview term: await using β€” the async counterpart, for a resource implementing [Symbol.asyncDispose]() instead β€” verified directly: the surrounding async function genuinely does not fully resolve until the async disposal itself has been correctly awaited.
  • A precise answer names DisposableStack/AsyncDisposableStack as the real, companion container types this proposal also introduces β€” a stack-based way to register MULTIPLE disposable resources together programmatically, beyond individual using declarations β€” confirmed directly to exist as real, defined globals alongside Symbol.dispose/Symbol.asyncDispose.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; directly answering both halves of the prompt (disposal-on-error, and reverse order) with real proof is the strong signal.

Code / implementation expected: Yes β€” real proof of both disposal-on-error and strict reverse-order disposal with 3 real resources is the clearest, most convincing demonstration.

  • es2024
  • 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

5 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 proof: using genuinely disposes a resource even when an error is thrown, and disposes multiple resources in strict reverse declaration order

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 94 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 nextMedium

How do getters and setters work?

Continue