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