Explicit Resource Management (using)
JS · Modern Featuressyntax
using resource = acquireResource();
await using resource = acquireAsyncResource();example
class TempFile {
constructor(path) {
this.path = path;
console.log(`Created: ${path}`);
}
[Symbol.dispose]() {
console.log(`Cleaned up: ${this.path}`);
}
}
{
using file = new TempFile("/tmp/data.txt");
// Use file here...
} // Automatically calls file[Symbol.dispose]() when block exitsoutput
"Created: /tmp/data.txt"
"Cleaned up: /tmp/data.txt"Note TC39 Stage 3 (expected in ES2025/2026). Similar to Python's 'with' or C#'s 'using'. Guarantees cleanup even if an error is thrown. Use Symbol.asyncDispose for async cleanup.