Introduction โ
@nodelibraries/enhanced-abort-controller is an enhanced version of the native AbortController with additional features for modern Node.js and TypeScript applications. It provides timeout-based cancellation, linked controllers, and proper resource cleanup patterns.
@nodelibraries/enhanced-abort-controller - Enhanced AbortController with Node.js-style patterns. Learn more in our Installation page.
Key Features โ
- ๐ฏ Type-safe - Full TypeScript support with type inference
- โฑ๏ธ Timeout Support - Automatic timeout-based cancellation with TimeSpan support
- ๐ Linked Controllers - Create controllers that abort when any linked signal aborts
- ๐ฆ Lightweight - Zero dependencies - pure TypeScript implementation
- ๐ Promise Support - Promise-based waiting with whenAborted for async workflows
- ๐งน Resource Cleanup - Automatic resource cleanup with registration callbacks
- โก Disposal Pattern - Proper disposal pattern for resource management
- ๐ก๏ธ Error Handling - Custom AbortError with proper error inheritance
What is AbortController? โ
AbortController is a Web API that provides a way to cancel ongoing operations. It's commonly used with fetch to cancel HTTP requests, but can be used with any async operation.
Native AbortController โ
typescript
const controller = new AbortController();
const signal = controller.signal;
fetch('https://api.example.com/data', { signal })
.then((response) => response.json())
.catch((err) => {
if (err.name === 'AbortError') {
console.log('Request was cancelled');
}
});
// Cancel the request
controller.abort();Enhanced AbortController โ
Our enhanced version adds powerful features while maintaining full compatibility with the native API:
typescript
import { EnhancedAbortController } from '@nodelibraries/enhanced-abort-controller';
const controller = new EnhancedAbortController();
// Auto-abort after 5 seconds
controller.abortAfter(5000);
// Register cleanup callback
controller.signal.register(() => {
console.log('Operation was aborted, cleaning up...');
});
fetch('https://api.example.com/data', {
signal: controller.signal.signal,
});Why Use Enhanced AbortController? โ
The enhanced version provides:
- Timeout Support - Automatically abort after a specified time
- Linked Controllers - Combine multiple abort signals
- TimeSpan - Precise time interval management
- Resource Cleanup - Automatic cleanup callbacks
- Promise Support -
whenAbortedpromise for async workflows - Better Error Handling - Custom AbortError with reasons
Comparison with Native AbortController โ
| Feature | Native | Enhanced |
|---|---|---|
| Basic abort | โ | โ |
| Timeout support | โ | โ |
| Linked signals | โ | โ |
| TimeSpan | โ | โ |
| Cleanup callbacks | โ | โ |
| Promise support | โ | โ |
| TypeScript | Partial | Full |