This repository accompanies my YouTube video on the new Explicit Resource Management feature in Node.js 24, which introduces the using
and await using
keywords for automatic resource cleanup.
📺 Watch the video: Node.js 24: Explicit Resource Management Explained
Node.js 24 (and newer) supports the new using
and await using
keywords, allowing you to automatically clean up resources (like files, servers, and streams) when they go out of scope. This is similar to features in languages like C# and Rust, and helps prevent resource leaks and boilerplate cleanup code.
-
Install Node.js 24
- This feature requires Node.js v24 or newer.
- If you use nvm:
nvm install 24 nvm use 24
-
Run the examples
- Each file is a standalone example. Run them with:
node <filename>
- Each file is a standalone example. Run them with:
-
- Demonstrates the basics of both synchronous and asynchronous resource disposal using
Symbol.dispose
andSymbol.asyncDispose
. Shows the syntax for bothusing
andawait using
keywords.
- Demonstrates the basics of both synchronous and asynchronous resource disposal using
-
- Creates a custom
Logger
class that writes to a file stream and implementsSymbol.asyncDispose
. When the logger goes out of scope, it automatically closes the stream and renames the log file with the current timestamp.
- Creates a custom
-
- Shows how to use
using
withsetInterval
. When the block ends, the interval is automatically cleared, preventing memory leaks and ongoing timer execution.
- Shows how to use
-
- Demonstrates using
await using
with Node.js's readline interface. The readline interface is automatically closed after use, without needing an explicitrl.close()
call.
- Demonstrates using
-
- Shows how to use
await using
with a child process spawned by Node.js. The child process is managed as a disposable resource, and its stdout is read using the newtoArray()
method from asyncIterators helpers.
- Shows how to use
This code is for educational purposes and accompanies my YouTube video. Feel free to fork, experiment, and share!
If you have questions or suggestions, leave a comment on the video or open an issue.
Happy coding! 🎉
- Check it here MIT