Sub topic 7
Optional Resources: null, undefined, and TypeErrors
This chapter assumes you already know how using declarations work, how disposal order is decided, and how await using awaits cleanup. Here we look at what happens when the value on the right-hand side is not a normal disposable object.
The rule: check at declaration, dispose at exit
When a using declaration runs, the engine looks at its value straight away. It does not wait until the block exits. Three outcomes are possible:
| Value | What happens at declaration | What happens at block exit |
|---|---|---|
| `null` or `undefined` | Accepted, nothing is recorded for cleanup | Nothing (sync) |
| Object with a callable `Symbol.dispose` | The method is looked up and stored | The stored method is called |
| Anything else | `TypeError` is thrown | Resources declared earlier are still disposed |
Optional resources with null and undefined
Because null and undefined are allowed, you can declare a resource that may not exist without writing a special branch for it:
function maybeOpen(enabled) {
if (!enabled) return null;
return {
[Symbol.dispose]() {
console.log("log file closed");
},
};
}
function run(enabled) {
using log = maybeOpen(enabled);
console.log(log ? "logging on" : "logging off");
}
run(true); // logging on, log file closed
run(false); // logging offInside the block you still have to check log before you use it. The using declaration only promises that cleanup is skipped safely.
Invalid values throw a TypeError
Any other value that is missing a callable Symbol.dispose throws a TypeError at the point of declaration. That includes primitives like 42 or "file", and plain objects with no dispose method. Resources declared earlier in the same block are still disposed, because the error leaves the block like any other exception:
const first = {
[Symbol.dispose]() {
console.log("first disposed");
},
};
try {
using a = first;
using b = { name: "not disposable" }; // TypeError here
console.log("never reached");
} catch (e) {
console.log(e instanceof TypeError); // true
}
// first disposed
// trueThrowing early is deliberate. If you write using f = openFile() and openFile returns the wrong kind of object, the code fails on that line, before the body runs. It does not wait and fail silently at cleanup time.
How await using differs
With await using, the engine first looks for Symbol.asyncDispose. If that method is missing, it falls back to Symbol.dispose and awaits the result. The value is rejected with a TypeError only when neither method exists.
null and undefined are accepted here too. There is one difference: block exit still performs an await even if every resource was null. The code after an await using block therefore always runs asynchronously, whether a resource was present or not, which keeps the timing predictable.
async function demo(resource) {
{
await using r = resource;
}
console.log("after block");
}
demo(null);
console.log("sync code");
// sync code
// after blockExercise
What does this code log, and in what order? try { using a = { [Symbol.dispose]() { console.log("a"); } }; using b = undefined; using c = "text"; console.log("body"); } catch (e) { console.log(e.constructor.name); }
Show answer
It logs "a", then "TypeError". The undefined value for b is accepted and skipped. The string "text" for c throws a TypeError at declaration, so "body" never runs. The error exits the block, which disposes a and logs "a". The catch then logs "TypeError".
Exercise
An object has only a Symbol.dispose method. Can it be used with await using, and is the code after the block synchronous?
Show answer
Yes. await using falls back to Symbol.dispose when Symbol.asyncDispose is missing. The block exit still awaits, so code after the block runs asynchronously.