DisposableStack.prototype.dispose() - JavaScript | MDN
Syntax
Parameters
None.
Return value
None (undefined).
Exceptions
SuppressedError-
Thrown if multiple disposers in the stack threw an error. If only one error is thrown, it is rethrown as-is. Otherwise, for each additional error, a new
SuppressedErroris created, with the original error as thesuppressedproperty, and the new error as theerrorproperty.
Examples
Disposing a stack
Here we push three disposers to the stack, using the use(), adopt(), and defer() methods. When dispose() is called, the disposers are called in reverse order of registration.
Note that usually you don't need to call dispose() manually. Declare the stack with using, and its [Symbol.dispose]() method will be automatically called when the stack goes out of scope.
js
class Resource {
dispose() {
console.log("Resource disposed");
}
[Symbol.dispose]() {
console.log("Resource disposed via Symbol.dispose");
}
}
{
const disposer = new DisposableStack();
const resource = disposer.use(new Resource());
const resource2 = disposer.adopt(new Resource(), (resource) =>
resource.dispose(),
);
disposer.defer(() => console.log("Deferred disposer"));
disposer.dispose();
// Logs in order:
// Deferred disposer
// Resource disposed
// Resource disposed via Symbol.dispose
}
Specifications
| Specification |
|---|
| ECMAScript Async Explicit Resource Management # sec-disposablestack.prototype.dispose |