{
  "id": 18242562,
  "title": "What JavaScript using disposes after returns and exceptions: nine checks",
  "url": "https://urgent.news/2026/11/05/what-javascript-using-disposes-after-returns-and-exceptions-nine",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-05T03:54:28.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/hirodeath/what-javascript-using-disposes-after-returns-and-exceptions-nine-checks-570f"
  },
  "original_language": "en",
  "account": "Nine checks in Node.js detail how JavaScript disposes of objects after returns and exceptions. Using registers objects with disposal methods in a scope, disposing them when the scope ends. Multiple resources follow reverse acquisition order for disposal. Nine conditions in Node.js cover disposal order, retained exceptions, and asynchronous waiting before use. Check the object supplying disposal and the scope exit that triggers cleanup before using.\n\nDisposal completes before the caller receives a return value. The provided code, executed as an .mjs file in Node.js 24.15.0, demonstrates this. Using creates objects with a Symbol.dispose method, which logs calls to the disposal process. Disposal of both objects finished before the return value was received.\n\nThe ECMAScript declaration and scope specification describe disposing resources registered through using when scope processing finishes. This includes failures as well as normal completion. Both disposals occurred in the order of acquisition, with A disposing before B.\n\nException handling in the body led to A disposing before entering the catch. B was disposed before throwing an error, while the body threw an error during disposal. Both exceptions were suppressed, retaining their messages. Cleanup continued even after a disposal failed, keeping earlier errors in a related chain.\n\nAn experiment with an asynchronously disposable value showed that it required await using for proper disposal. An object with only Symbol.asyncDispose in a using declaration caused a TypeError. Asynchronous disposal needed separate handling, as a real Node.js temporary directory demonstrated. The directory and its contents were removed when the exception left the scope. Node.js 24.15.0 introduced mkdtempDisposableSync, which returns an object with a path and disposal method for removal.",
  "summary": "using registers an object with a disposal method in a scope and calls cleanup when that scope ends. When several resources were registered, they were disposed in reverse acquisition order. An early return followed the same rule. Nine conditions in Node.js covered disposal order, retained exceptions, and asynchronous waiting. Before using it, check which object supplies disposal and which scope…",
  "key_points": [
    "JavaScript disposes objects after returns and exceptions in Node.js",
    "Scope exit triggers cleanup of disposed objects before return value",
    "Nine conditions cover disposal order, exceptions, and asynchronous waiting"
  ],
  "editors_take": "Standardizing these disposal rules ensures that resource cleanup remains predictable and reliable even when developers encounter unexpected errors or complex asynchronous operations during execution.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}