Skip to content

Explicit Resource Management

Spicy — senior dev territorylanguages

ELI5 - The Vibe Check

Explicit Resource Management gives JavaScript a using keyword that automatically cleans things up when they go out of scope, like closing a file handle or an AbortController, without you writing a try/finally every single time. It's the same idea as Python's with block or C#'s using statement, finally landing in JS.

Real Talk

Explicit Resource Management is a TC39 feature, shipped in TypeScript 5.2+ and modern JS engines, that introduces using and await using declarations, which automatically call a resource's Symbol.dispose or Symbol.asyncDispose method when the enclosing block exits, even on early return or thrown error. It formalizes deterministic cleanup for things like database connections, file handles, and network resources.

Show Me The Code

function readFile(path) {
  using file = openFile(path); // auto-closed at block end
  return file.read();
} // file.close() called here automatically, even on throw

When You'll Hear This

"We swapped the try/finally boilerplate for a using declaration and the cleanup is guaranteed either way." / "await using needs Symbol.asyncDispose implemented on the resource, plain Promises won't work."

Made with passive-aggressive love by manoga.digital. Powered by Claude.