Adds a callback to be invoked when the stack is disposed.
Disposes each resource in the stack in the reverse order that they were added.
Move all resources out of this stack and into a new DisposableStack
, and marks this stack as disposed.
class C {
#res1: Disposable;
#res2: Disposable;
#disposables: DisposableStack;
constructor() {
// stack will be disposed when exiting constructor for any reason
using stack = new DisposableStack();
// get first resource
this.#res1 = stack.use(getResource1());
// get second resource. If this fails, both `stack` and `#res1` will be disposed.
this.#res2 = stack.use(getResource2());
// all operations succeeded, move resources out of `stack` so that they aren't disposed
// when constructor exits
this.#disposables = stack.move();
}
[Symbol.dispose]() {
this.#disposables.dispose();
}
}
Returns a value indicating whether this stack has been disposed.