Skip to content

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:

  1. Timeout Support - Automatically abort after a specified time
  2. Linked Controllers - Combine multiple abort signals
  3. TimeSpan - Precise time interval management
  4. Resource Cleanup - Automatic cleanup callbacks
  5. Promise Support - whenAborted promise for async workflows
  6. Better Error Handling - Custom AbortError with reasons

Comparison with Native AbortController โ€‹

FeatureNativeEnhanced
Basic abortโœ…โœ…
Timeout supportโŒโœ…
Linked signalsโŒโœ…
TimeSpanโŒโœ…
Cleanup callbacksโŒโœ…
Promise supportโŒโœ…
TypeScriptPartialFull

Next Steps โ€‹

Released under the MIT License. If you find this project helpful, consider buying me a coffee โ˜•