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.disposeandSymbol.asyncDispose. Shows the syntax for bothusingandawait usingkeywords.
- Demonstrates the basics of both synchronous and asynchronous resource disposal using
-
- Creates a custom
Loggerclass 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
usingwithsetInterval. When the block ends, the interval is automatically cleared, preventing memory leaks and ongoing timer execution.
- Shows how to use
-
- Demonstrates using
await usingwith 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 usingwith 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