ProgramGuru

Resource Management with using and await using

Resource Management with using and await using — the core idea

using registers synchronous cleanup via Symbol.dispose when a scope exits. await using supports asynchronous cleanup with Symbol.asyncDispose. The declarations run at runtime after compilation; unlike types, resource management has real control-flow effects.

What you’ll learn

  • Explain resource management with using and await using with a concrete use case.
  • Run the example and account for every line of its output.
  • Recognize the difference between compile-time checking and runtime behavior.

Understand the rules and when to use them

Reading the code: The first block logs its work then calls Symbol.dispose at scope exit. The second block awaits Symbol.asyncDispose before the async function proceeds. Cleanup is deterministic with respect to each scope, not a garbage-collection event.

Keep the boundary clear: For older environments, symbols and related disposal helpers may need polyfills. Compile with lib esnext.disposable and verify runtime support; a type declaration alone cannot provide cleanup.

See the idea as a flow

Resource Management with using and await using — conceptual flowusing → scope end then Symbol.dispose then await using → async cleanupResource Management with using and await using1using → scope end2Symbol.dispose3await using → async cleanupAsynchronous operations may resume after the current JavaScript stack.
Follow the three stages and identify what happens in TypeScript versus JavaScript.

Run the complete TypeScript example

Save the code as resource-management-using-await-using.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.

TypeScript code

async function main(): Promise<void> {
  {
    using resource = {
      [Symbol.dispose]() { console.log("closed sync"); }
    };
    console.log("using resource");
  }
  {
    await using resource = {
      async [Symbol.asyncDispose]() { console.log("closed async"); }
    };
    console.log("await using resource");
  }
}
main().catch(console.error);

Verified output

using resource
closed sync
await using resource
closed async

Build: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable resource-management-using-await-using.ts; run: node resource-management-using-await-using.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.

Trace the example line by line

The first block logs its work then calls Symbol.dispose at scope exit. The second block awaits Symbol.asyncDispose before the async function proceeds. Cleanup is deterministic with respect to each scope, not a garbage-collection event.

The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.

Avoid this common mistake

For older environments, symbols and related disposal helpers may need polyfills. Compile with lib esnext.disposable and verify runtime support; a type declaration alone cannot provide cleanup.

Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.

Practice independently

Create two resources in one scope and observe disposal order (the last acquired resource is disposed first).

Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.

Check your understanding

When is a using resource disposed?
Choose one answer. You can try again.

Answer and explanation

At scope exit, even on early return or exception (subject to normal cleanup semantics).

The first block logs its work then calls Symbol.dispose at scope exit. The second block awaits Symbol.asyncDispose before the async function proceeds. Cleanup is deterministic with respect to each scope, not a garbage-collection event.

What to remember

using registers synchronous cleanup via Symbol.

Try the practice change and re-run the exact program to test your understanding.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: TypeScript in the Browser →