JavaScript using Declaration
The using declaration is a new feature that is not yet supported in every browser. Check browser support before using it in production.
The using declaration creates a block-scoped constant for a resource that is cleaned up automatically, by calling its Symbol.dispose method, when the block ends.
Description
The using declaration declares a block-scoped, read-only variable for a resource that needs cleaning up, such as a file handle, lock or subscription. When the enclosing block exits, whether normally or because of an error, JavaScript automatically calls the resource's [Symbol.dispose]() method. await using does the same with [Symbol.asyncDispose](), awaiting it.
Resources are disposed in the reverse order they were declared. It replaces many try...finally clean-up blocks. This feature, called explicit resource management, is new, so check support before relying on it.
A resource for using is any object with a [Symbol.dispose]() method (or null or undefined, which are simply skipped). If disposing throws an error while another error is already being thrown, both are kept in a SuppressedError. The DisposableStack and AsyncDisposableStack objects let you collect several resources, or clean-up callbacks, and dispose of them together, which is useful when the number of resources isn't known in advance.
Syntax
using name = resource;
await using name = asyncResource;Example
Browser Support
The following information will show you the current browser support for the using declaration. Hover over a browser icon to see the version that first introduced support for this declaration.
Desktop





Tablets & Mobile





