Abstract
Protected
constructorOptional
finalizer: () => null | voidProtected
disposeCurrent disposal state.
True if object is disposed or disposing.
Protected
_Protected
Completes disposal process and executes finalizer.
Protected
_Protected
Called before disposal starts. Override in derived classes.
Protected
_Is called when this object is disposed. Should NOT be called directly. Override this method to handle disposal.
Protected
_Protected
Initiates disposal process. Returns false if already disposed.
Protected
assertProtected
Throws ObjectDisposedException if object is disposed.
When true, throws if not in Alive state.
Asynchronously releases all resources used by the object.
This method should be idempotent - calling it multiple times should have no additional effect after the first call completes. Implementations should:
A Promise that resolves when disposal is complete.
class FileBuffer implements AsyncDisposable {
async disposeAsync(): Promise<void> {
await this.flush(); // Write pending data
await this.close(); // Close file handle
}
}
// Proper async disposal
const buffer = new FileBuffer();
try {
// Use buffer...
} finally {
await buffer.disposeAsync(); // Wait for complete cleanup
}
Defines the contract for objects that can be disposed asynchronously to release resources.
This interface is for objects that require asynchronous operations during disposal, such as closing database connections, flushing buffers, or performing network cleanup. This allows for simple type checking that includes types that don't explicitly declare themselves as implementing this interface but do have a disposeAsync() method.
Example