Creating Custom Events

What patterns should be used for handling asynchronous operations with custom events?
class AsyncEventHandler {
  constructor() {
    this.pending = new Map();
  }

  async handleAsyncEvent(e) {
    const requestId = Date.now();
    this.pending.set(requestId, true);

    try {
      const result = await this.processAsync(e.detail);
      
      if (this.pending.get(requestId)) {
        this.dispatchResult('asyncComplete', result);
      }
    } catch (error) {
      if (this.pending.get(requestId)) {
        this.dispatchResult('asyncError', error);
      }
    } finally {
      this.pending.delete(requestId);
    }
  }

  cancel(requestId) {
    this.pending.delete(requestId);
  }
}
Next Question (16/20)