JavaScript's New Superpower: Explicit Resource Management

https://news.ycombinator.com/rss Hits: 6
Summary

The Explicit Resource Management proposal introduces a deterministic approach to explicitly manage the lifecycle of resources like file handles, network connections, and more. This proposal brings the following additions to the language: the using and await using declarations, which automatically calls dispose method when a resource goes out of scope; [Symbol.dispose]() and [Symbol.asyncDispose]() symbols for cleanup operations; two new global objects DisposableStack and AsyncDisposableStack as containers to aggregate disposable resources; and SuppressedError as a new type of error (contain both the error that was most recently thrown, as well as the error that was suppressed) to address the scenario where an error occurs during the disposal of a resource, and potientially masking an existing error thrown from the body, or from the disposal of another resource. These additions enable developers to write more robust, performant, and maintainable code by providing fine-grained control over resource disposal.using and await using declarations #The core of the Explicit Resource Management proposal lies in the using and await using declarations. The using declaration is designed for synchronous resources, ensuring that the [Symbol.dispose]() method of a disposable resource is called when the scope in which it's declared exits. For asynchronous resources, the await using declaration works similarly, but ensures that the [Symbol.asyncDispose]() method is called and the result of this calling is awaited, allowing for asynchronous cleanup operations. This distinction enables developers to reliably manage both synchronous and asynchronous resources, preventing leaks and improving overall code quality. The using and await using keywords can be used inside braces {} (such as blocks, for loops and function bodies), and cannot be used in top-levels.For example, when working with ReadableStreamDefaultReader, it's crucial to call reader.releaseLock() to unlock the stream and allow ...

First seen: 2025-05-17 07:46

Last seen: 2025-05-17 12:47