AsyncDisposableStack
The AsyncDisposableStack global object collects cleanup actions that may be asynchronous and awaits each of them, in reverse order, when the stack is disposed. It’s the asynchronous counterpart of DisposableStack and pairs with an await using declaration. Each deferred callback below returns a promise, and .disposeAsync() waits for them, the last one first:
> const cleanup = new AsyncDisposableStack();
undefined
> cleanup.defer(async () => console.log("Flushing upload buffer"));
undefined
> cleanup.defer(async () => console.log("Closing network socket"));
undefined
> await cleanup.disposeAsync()
Closing network socket
Flushing upload buffer
undefined
A stack accepts synchronous cleanup too, so one stack can hold a database connection, a client with its own close method, and a plain lock together. Calling .move() hands every registered action to a new stack, which lets a function return resources it opened without closing them.
Note: AsyncDisposableStack is part of the explicit resource management proposal, which reached Stage 4 in May 2026 and is expected in ES2027. It ships in Chrome and Edge 134, Firefox 141, and Node.js 24, but as of 2026 not yet in a stable Safari release, so it has Limited availability in Baseline terms.
Syntax
The AsyncDisposableStack constructor takes no arguments:
new AsyncDisposableStack()
The constructor returns a new, empty stack whose .disposed property is false. The new keyword is required, and calling AsyncDisposableStack() without it throws a TypeError.
Key Instance Methods and Properties
| Member | Kind | Description |
|---|---|---|
.use() |
Instance method | Registers a value’s [Symbol.asyncDispose]() method, or its [Symbol.dispose]() method if it has no async one, and returns the value |
.adopt() |
Instance method | Registers a callback that receives the given value at disposal time, and returns the value |
.defer() |
Instance method | Registers a callback that takes no arguments, and returns undefined |
.move() |
Instance method | Transfers every registered action to a new stack and marks the original as disposed without running anything |
.disposeAsync() |
Instance method | Runs every registered action in reverse order and returns a promise that settles once they finish |
[Symbol.asyncDispose]() |
Instance method | Does the same as .disposeAsync(), which lets await using dispose of the stack |
.disposed |
Instance property | Returns true once the stack has been disposed or moved, and false before that |
Examples
A request handler opens a database connection with a [Symbol.asyncDispose]() method, a cache client that only has an async .quit() method, and a lock that cleans up synchronously. One stack declared with await using releases all three when the function returns:
> function connect(name) {
… console.log(`Connected to ${name}`);
… return {
… name,
… async [Symbol.asyncDispose]() {
… console.log(`Disconnected from ${name}`);
… },
… };
… }
undefined
> function createCacheClient() {
… console.log("Cache client ready");
… return { quit: async () => console.log("Cache client quit") };
… }
undefined
> async function handleRequest() {
… await using stack = new AsyncDisposableStack();
… const db = stack.use(connect("orders-db"));
… stack.adopt(createCacheClient(), async (client) => client.quit());
… stack.use({ [Symbol.dispose]: () => console.log("Released lock") });
… console.log(`Querying ${db.name}`);
… }
undefined
> await handleRequest()
Connected to orders-db
Cache client ready
Querying orders-db
Released lock
Cache client quit
Disconnected from orders-db
undefined
Step through handleRequest() to watch the stack collect each cleanup action, then await them one at a time from the top down:
A service opens a pool of connections at startup, and any connection can fail. The stack disconnects the ones already open when a later one fails. On success, .move() hands the open connections to the caller, which disposes of the pool with its own await using declaration:
open-pool.mjs
function connect(name) {
if (name === "replica") {
throw new Error(`Can't reach ${name}`);
}
console.log(`Connected to ${name}`);
return {
name,
async [Symbol.asyncDispose]() {
console.log(`Disconnected from ${name}`);
},
};
}
async function openPool(names) {
await using stack = new AsyncDisposableStack();
const connections = names.map((name) => stack.use(connect(name)));
const owned = stack.move();
return { connections, [Symbol.asyncDispose]: () => owned.disposeAsync() };
}
try {
await openPool(["primary", "replica"]);
} catch (error) {
console.log(`Failed: ${error.message}`);
}
await using pool = await openPool(["primary", "cache"]);
console.log(`Pool size: ${pool.connections.length}`);
The program uses top-level await using, which works only in a module, so it runs as a file:
$ node open-pool.mjs
Connected to primary
Disconnected from primary
Failed: Can't reach replica
Connected to primary
Connected to cache
Pool size: 2
Disconnected from cache
Disconnected from primary
Common Gotchas
The stack awaits each action before it starts the next one, so slow cleanup steps add up instead of overlapping:
> const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
undefined
> const stack = new AsyncDisposableStack();
undefined
> for (const name of ["first", "second"]) {
… stack.defer(async () => {
… console.log(`Closing ${name}`);
… await wait(100);
… console.log(`Closed ${name}`);
… });
… }
undefined
> await stack.disposeAsync()
Closing second
Closed second
Closing first
Closed first
undefined
An AsyncDisposableStack has no [Symbol.dispose]() method, so declaring it with a plain using throws a TypeError:
> {
… using stack = new AsyncDisposableStack();
… }
Uncaught TypeError: Symbol(Symbol.dispose) is not a function
When more than one action fails, the promise from .disposeAsync() rejects with a SuppressedError that holds the errors, nesting another SuppressedError for each failure past the second, the same way DisposableStack reports them.
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 13, 2026