move
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();
}
}Content copied to clipboard