Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    JavaScript Explicit Resource Management

    JavaScript Tutorials


    JavaScript explicit resource management gives you a structured way to release files, locks, connections, and other limited resources. You declare a resource with using or await using, and JavaScript runs its cleanup method when control leaves the scope.

    This approach makes cleanup easier to see and harder to forget. It also runs when the block ends because of an error, return, break, or continue.

    Why Explicit Resource Management Matters

    Garbage collection reclaims memory, but it does not guarantee timely cleanup of external resources. A database connection, file handle, stream lock, or temporary directory may need to close at a specific point. Traditionally, you place that work in finally.

    Traditional cleanup:

    const connection = openConnection();
    
    try {
        // Use the resource inside the protected block
        connection.query("SELECT * FROM orders");
    } finally {
        // Always release the connection
        connection.close();
    }

    The pattern works, but nested resources create more cleanup code. Explicit resource management connects a resource to its lexical scope, so the language manages the final step.

    Core Parts of the Feature

    Part Purpose Cleanup method
    using Manages a synchronous resource Symbol.dispose
    await using Manages an asynchronous resource Symbol.asyncDispose
    DisposableStack Groups several synchronous cleanup actions dispose()
    AsyncDisposableStack Groups asynchronous cleanup actions disposeAsync()

    Version note: Check support in your JavaScript runtime before using this syntax in production. Some browsers and older runtimes do not yet support every part of the feature.

    Create a Synchronous Disposable Resource

    An object works with using when it provides a method keyed by Symbol.dispose. The method should release the resource without returning a pending asynchronous operation.

    Example:

    class ReportFile {
        constructor(name) {
            this.name = name;
            console.log("Opened " + name);
        }
    
        read() {
            // Simulate reading a local report
            console.log("Reading " + this.name);
        }
    
        [Symbol.dispose]() {
            // Release the resource when the block ends
            console.log("Closed " + this.name);
        }
    }
    
    {
        using report = new ReportFile("sales.csv");
        report.read();
    }

    Output:

    Opened sales.csv
    Reading sales.csv
    Closed sales.csv

    The extra braces create a clear block scope. When execution reaches the closing brace, JavaScript calls the saved disposal method automatically.

    Understand Cleanup Order

    JavaScript disposes multiple resources in the reverse order of declaration. This last-in, first-out order helps when a later resource depends on an earlier one.

    Example:

    function resource(name) {
        return {
            name,
            [Symbol.dispose]() {
                // Show the order in which cleanup runs
                console.log("Disposed " + name);
            }
        };
    }
    
    {
        using database = resource("database");
        using transaction = resource("transaction");
        console.log("Work completed");
    }

    Output:

    Work completed
    Disposed transaction
    Disposed database

    Use await using for Asynchronous Cleanup

    Use await using when cleanup must wait for an asynchronous operation. The resource should implement Symbol.asyncDispose. You can use the declaration inside an async function or another context that permits await.

    Example:

    class ApiSession {
        constructor(user) {
            this.user = user;
            console.log("Session opened for " + user);
        }
    
        async fetchOrders() {
            // Simulate an asynchronous request
            await Promise.resolve();
            console.log("Orders loaded");
        }
    
        async [Symbol.asyncDispose]() {
            // Wait until the remote session closes
            await Promise.resolve();
            console.log("Session closed");
        }
    }
    
    async function loadDashboard() {
        await using session = new ApiSession("Aarav");
        await session.fetchOrders();
    }
    
    await loadDashboard();

    Output:

    Session opened for Aarav
    Orders loaded
    Session closed

    await using does not wait while declaring the variable. It waits for disposal when the scope exits. Asynchronous resources are cleaned up sequentially in reverse declaration order.

    Manage Existing Resources with DisposableStack

    You may need to register several values or cleanup callbacks conditionally. DisposableStack provides three useful methods:

    • use(value): registers an object that already implements the disposable protocol.
    • adopt(value, callback): attaches cleanup logic to a value that is not disposable.
    • defer(callback): registers a cleanup action that does not need a value.

    Example:

    {
        using cleanup = new DisposableStack();
    
        const timerId = setInterval(() => {
            console.log("Checking queue");
        }, 1000);
    
        cleanup.defer(() => {
            // Stop the timer when this block ends
            clearInterval(timerId);
            console.log("Timer stopped");
        });
    
        console.log("Queue monitor started");
    }

    Output:

    Queue monitor started
    Timer stopped

    Handle Cleanup Errors

    If both the main operation and disposal fail, JavaScript can represent the combined failure with SuppressedError. Its error property contains the disposal error, while suppressed contains the earlier error. Catch errors outside the resource scope when you need to inspect both.

    Tip: Keep disposal methods focused and safe. A cleanup method should release its resource once and avoid starting unrelated work.

    Best Practices

    • Use the smallest practical block so the resource has a clear lifetime.
    • Choose using for synchronous cleanup and await using only when cleanup returns a promise.
    • Do not expect let or const to trigger disposal.
    • Allow null or undefined when resource acquisition is optional; JavaScript safely skips disposal for these values.
    • Test error paths to confirm that each resource closes in the expected order.
    • Keep a try...finally fallback when your target runtimes do not support the feature.

    Conclusion

    JavaScript explicit resource management ties cleanup to scope through using, await using, and the disposable protocols. It reduces repeated finally blocks, preserves predictable reverse-order cleanup, and makes resource ownership clear. Use it when a resource must close promptly, and verify runtime support before deployment.



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.