Skip to content
📚 4 min read

Examples Overview ​

This section provides comprehensive, practical examples of all async patterns and implementations covered in this project. Each example is designed to demonstrate real-world usage and best practices.

Promise Examples ​

Learn how to use various Promise implementations:

typescript
// Custom Promise Example
const myPromise = new CustomPromise((resolve, reject) => {
  setTimeout(() => resolve('Success!'), 1000);
});

// Promise.all with API Calls
const results = await Promise.all([
  fetch('/api/users'),
  fetch('/api/posts'),
  fetch('/api/comments'),
]);

// Promise.race with Timeout
const result = await Promise.race([
  fetch('/api/data'),
  new Promise((_, reject) =>
    setTimeout(() => reject(new Error('Timeout')), 5000)
  ),
]);

Task Pattern Examples ​

Implement common async task patterns:

typescript
// Sequential Task Processing
async function processInSeries(tasks: Array<() => Promise<any>>) {
  const results = [];
  for (const task of tasks) {
    results.push(await task());
  }
  return results;
}

// Parallel Task Processing with Concurrency Limit
async function processInParallel(
  tasks: Array<() => Promise<any>>,
  concurrency = 3
) {
  const results = [];
  const running = new Set();

  for (const task of tasks) {
    if (running.size >= concurrency) {
      await Promise.race(running);
    }
    const promise = task().then((result) => {
      results.push(result);
      running.delete(promise);
    });
    running.add(promise);
  }

  await Promise.all(running);
  return results;
}

Timer Examples ​

Enhanced timer implementations:

typescript
// Custom setTimeout with Cleanup
const timer = new Timer();
const cleanup = timer.setTimeout(() => {
  console.log('Executed after delay');
}, 1000);

// Cleanup when done
cleanup();

// Custom setInterval with Pause/Resume
const interval = new ControlledInterval(() => {
  console.log('Tick');
}, 1000);

interval.pause(); // Pause execution
interval.resume(); // Resume execution
interval.clear(); // Stop completely

Advanced Pattern Examples ​

Sophisticated async patterns for real-world scenarios:

typescript
// Auto-Retry with Exponential Backoff
const result = await withRetry(
  async () => {
    return await fetch('/api/data');
  },
  {
    maxAttempts: 3,
    backoffFactor: 2,
    initialDelay: 1000,
  }
);

// Debounced API Call
const debouncedSearch = debounce(async (query: string) => {
  const results = await fetch(`/api/search?q=${query}`);
  return results.json();
}, 300);

// Memoized Expensive Operation
const memoizedFetch = memoize(
  async (id: string) => {
    return await fetch(`/api/user/${id}`);
  },
  {
    ttl: 60000, // 1 minute
    maxSize: 100,
  }
);

Performance Examples ​

Optimize async operations:

typescript
// Memory-Efficient Stream Processing
async function processLargeDataStream(stream: ReadableStream) {
  const reader = stream.getReader();
  const monitor = new MemoryMonitor();

  try {
    while (true) {
      const { done, value } = await reader.read();
      if (done) break;

      monitor.checkMemoryUsage();
      await processChunk(value);
    }
  } finally {
    reader.releaseLock();
  }
}

// Performance Monitoring
const monitor = new PerformanceMonitor();
monitor.onMetric(({ operationName, duration }) => {
  if (duration > 1000) {
    console.warn(`Slow operation: ${operationName}`);
  }
});

await monitor.track('fetchData', async () => {
  return await fetch('/api/data');
});

Real-World Examples ​

Complete implementations of common scenarios:

typescript
// API Client with Retries, Caching, and Rate Limiting
class APIClient {
  private memoizer = new Memoizer({ maxAge: 60000 });
  private rateLimiter = new RateLimiter({
    maxRequests: 100,
    timeWindow: 60000,
  });

  async fetchUser(id: string) {
    return this.memoizer.memoize(
      () =>
        withRetry(
          async () => {
            await this.rateLimiter.acquire();
            try {
              const response = await fetch(`/api/users/${id}`);
              if (!response.ok) throw new Error(`HTTP ${response.status}`);
              return response.json();
            } finally {
              this.rateLimiter.release();
            }
          },
          { maxAttempts: 3 }
        ),
      id
    );
  }
}

// Event Processing System
class EventProcessor {
  private queue: Array<Event> = [];
  private processing = false;
  private batchSize = 10;
  private processInterval = 1000;

  async start() {
    this.processing = true;
    while (this.processing) {
      const batch = this.queue.splice(0, this.batchSize);
      if (batch.length === 0) {
        await new Promise((resolve) =>
          setTimeout(resolve, this.processInterval)
        );
        continue;
      }

      await Promise.all(batch.map((event) => this.processEvent(event)));
    }
  }

  private async processEvent(event: Event) {
    try {
      await this.validateEvent(event);
      await this.transformEvent(event);
      await this.persistEvent(event);
    } catch (error) {
      await this.handleError(error, event);
    }
  }

  stop() {
    this.processing = false;
  }
}

Each example category has its own dedicated page with more detailed explanations, use cases, and best practices. Navigate through the sidebar to explore specific examples in depth.