DisposableStack
The DisposableStack global object collects cleanup actions and runs all of them, in reverse order, when the stack is disposed. It handles cleanup that a using declaration can’t express on its own, such as a value without a [Symbol.dispose]() method or a number of resources only known at run time. Each deferred callback below runs when .dispose() is called, the last one first:
> const cleanup = new DisposableStack();
undefined
> cleanup.defer(() => console.log("Deleting temp file"));
undefined
> cleanup.defer(() => console.log("Closing log"));
undefined
> cleanup.dispose()
Closing log
Deleting temp file
undefined
> cleanup.disposed
true
Despite its name, a DisposableStack has no method to pop a single entry. Disposal always runs every registered action. A stack is itself disposable, so it’s often declared with using and cleans up when its block exits. The asynchronous counterpart is AsyncDisposableStack.
Note: DisposableStack 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 DisposableStack constructor takes no arguments:
new DisposableStack()
The constructor returns a new, empty stack whose .disposed property is false. The new keyword is required, and calling DisposableStack() without it throws a TypeError.
Key Instance Methods and Properties
| Member | Kind | Description |
|---|---|---|
.use() |
Instance method | Registers a value’s own [Symbol.dispose]() method and returns the value. Accepts null and undefined and ignores them |
.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 |
.dispose() |
Instance method | Runs every registered action in reverse order. Does nothing if the stack is already disposed |
[Symbol.dispose]() |
Instance method | Does the same as .dispose(), which lets using dispose of the stack |
.disposed |
Instance property | Returns true once the stack has been disposed or moved, and false before that |
Examples
An order service locks a record with an object that has a .release() method but no [Symbol.dispose](). The .adopt() method pairs the lock with its release step, so the lock is freed when the block exits:
> function acquireLock(name) {
… console.log(`Locked ${name}`);
… return { name, release: () => console.log(`Released ${name}`) };
… }
undefined
> {
… using stack = new DisposableStack();
… const lock = stack.adopt(acquireLock("orders"), (lock) => lock.release());
… console.log(`Updating ${lock.name}`);
… }
Locked orders
Updating orders
Released orders
undefined
A batch job opens several files, and any file can fail to open. The stack closes the files already opened when a later one fails. On success, .move() hands the open files to the caller instead of closing them at the end of the function:
> function openFile(name) {
… if (name.endsWith(".lock")) {
… throw new Error(`Can't open ${name}`);
… }
… console.log(`Opened ${name}`);
… return { name, [Symbol.dispose]: () => console.log(`Closed ${name}`) };
… }
undefined
> function openAll(names) {
… using stack = new DisposableStack();
… const files = names.map((name) => stack.use(openFile(name)));
… const owned = stack.move();
… return { files, [Symbol.dispose]: () => owned.dispose() };
… }
undefined
> try {
… openAll(["a.txt", "b.lock"]);
… } catch (error) {
… console.log(error.message);
… }
Opened a.txt
Closed a.txt
Can't open b.lock
undefined
> {
… using batch = openAll(["a.txt", "b.txt"]);
… console.log(`Processing ${batch.files.length} files`);
… }
Opened a.txt
Opened b.txt
Processing 2 files
Closed b.txt
Closed a.txt
undefined
After .move(), the stack declared in openAll() is already marked as disposed, so the using declaration closes nothing when the function returns.
Step through both calls to openAll() below to watch what each stack holds, what .move() hands over, and which file closes first:
Common Gotchas
Once a stack is disposed, registering anything else on it throws a ReferenceError, while a second .dispose() call quietly does nothing:
> const stack = new DisposableStack();
undefined
> stack.dispose()
undefined
> stack.defer(() => {})
Uncaught:
ReferenceError: Cannot call DisposableStack.prototype.defer on an already-disposed DisposableStack
at DisposableStack.defer (<anonymous>)
> stack.dispose()
undefined
A failing cleanup action doesn’t stop the others from running. When more than one fails, .dispose() throws a SuppressedError. Its .error property holds the later error, and its .suppressed property holds the earlier one:
> const stack = new DisposableStack();
undefined
> stack.defer(() => {
… throw new Error("Flush failed");
… });
undefined
> stack.defer(() => {
… throw new Error("Close failed");
… });
undefined
> try {
… stack.dispose();
… } catch (error) {
… console.log(`${error.name}: ${error.error.message}`);
… console.log(`Suppressed: ${error.suppressed.message}`);
… }
SuppressedError: Flush failed
Suppressed: Close failed
undefined
The actions run in reverse order, so the callback registered last fails first and ends up as the suppressed error.
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