Sub topic 9
Where using Is Allowed and What It Forbids
This chapter assumes you already know how using and await using dispose resources at block exit, how let/const block scoping works, and where await is legal. In this chapter, using refers to both forms unless the text says otherwise.
The working model
A using declaration is a const declaration with cleanup attached. The engine has to know exactly which scope owns the resource, and the binding must not change before disposal. Every restriction below follows from those two requirements.
Where you can write using
The examples use a small helper so they run as shown:
function openResource(name) {
return {
name,
[Symbol.dispose]() {
console.log(`close ${name}`);
},
};
}
// 1. Any block
{
using a = openResource("block");
}
// 2. Function bodies
function work() {
using b = openResource("function");
return b.name;
}
work();
// 3. Class static blocks
class Config {
static {
using c = openResource("static block");
}
}
// 4. Classic for loop head: disposed once, when the loop ends
for (using d = openResource("for head"), i = { [Symbol.dispose]() {} }; false; ) {}
// 5. for...of head: disposed at the end of each iteration
for (using e of [openResource("of 1"), openResource("of 2")]) {}await using is allowed wherever await is allowed: inside async functions, in async arrow functions, at the top level of an ES module, and in for await (await using x of ...) loops.
What is forbidden
| Construct | Example | Why |
|---|---|---|
| Top level of a classic script | `using x = r;` in a non-module `<script>` | A script has no block whose exit would trigger disposal |
| Directly in a `case` clause | `case 1: using x = r;` | Case clauses share one scope, so control can jump past the declaration |
| Destructuring | `using { a } = r;` | The disposed value must be one binding |
| Missing initializer | `using x;` | There is nothing to dispose |
| Reassignment | `x = other;` | The binding is immutable, like `const` (TypeError) |
| `for...in` head | `for (using k in obj)` | Keys are strings, not resources |
| `export` | `export using x = r;` | A module export would outlive the module body's scope |
| `await using` in sync code | inside a plain function | `await` is not available there |
You fix most of these by adding an explicit block:
function handle(kind) {
switch (kind) {
case "a": {
using r = openResource("case a");
console.log("handling", r.name);
break;
}
default:
console.log("nothing to open");
}
}
handle("a");When you need a property of a resource, bind the whole resource first and destructure afterwards with const { name } = r;.
Contextual keyword gotchas
using is not a reserved word. Old code that uses using as a variable still works, so the parser treats it as a declaration only in narrow cases:
- An identifier must follow on the same line. If you write
usingand thenx = ron the next line, the parser reads an expression statementusing;followed by an assignment. await usingalso requiresawaitandusingto be on the same line.for (using of items)treatsusingas a plain variable name. It is not a declaration of a variable namedof.using[0]andusing.fooare ordinary member accesses on a variable namedusing.
Common errors
- Moving a working module snippet into a classic script and getting a SyntaxError at the top level.
- Prettier-style line breaks after
usingthat silently change the meaning of the code. Keep the declaration on one line. - Writing
for (using k in obj)when you meantfor...of.
Exercise
Which of these are legal? (1) using { db } = openDb(); inside a function, (2) case 2: using t = openTemp(); break;, (3) await using c = await connect(); at the top level of an ES module, (4) for (using f of files) {} inside a regular function.
Show answer
(1) is illegal because using cannot destructure. (2) is illegal because the declaration sits directly in a case clause; wrap the clause body in braces to fix it. (3) is legal because modules allow top-level await. (4) is legal, and each f is disposed at the end of its iteration.
Exercise
A teammate writes using on one line and conn = open(); on the next. Why is conn never disposed?
Show answer
A using declaration requires the identifier on the same line as using. With the line break, the parser reads an expression statement using; and then an ordinary assignment to conn, so no disposal is registered. It may also throw a ReferenceError if no variable named using exists.