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:
usingdeclaration — declares a resource that must implement[Symbol.dispose](); the engine genuinely, automatically calls that method when the enclosing block exits — normally OR via an earlyreturn/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
usingresource 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 surroundingasync functiongenuinely does not fully resolve until the async disposal itself has been correctlyawaited. - A precise answer names
DisposableStack/AsyncDisposableStackas the real, companion container types this proposal also introduces — a stack-based way to register MULTIPLE disposable resources together programmatically, beyond individualusingdeclarations — confirmed directly to exist as real, defined globals alongsideSymbol.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.