datadog-ux-utils Documentation - v1.0.0
    Preparing search index...

    Function dedupe

    • Deduplicate identical async calls and (optionally) cache results for a short TTL.

      Backward-compatible signatures: dedupe(key, op) // no caching, no telemetry dedupe(key, op, 5000) // ttlMs as number dedupe(key, op, { ttlMs: 5000, report: true }) // with options & telemetry

      Type Parameters

      • T

      Parameters

      • key: string

        Unique key for the operation (e.g., 'GET /api/items?page=1')

      • op: () => Promise<T>

        Function that returns a Promise

      • OptionaloptionsOrTtl: number | DedupeOptions

        Either a number (ttlMs) or an options object

      Returns Promise<T>

      The resolved value from the operation, or cached value if available.

      // Deduplicate concurrent fetches
      async function loadPatients() {
      return dedupe('GET:/api/patients', async () => {
      const resp = await fetch('/api/patients');
      return resp.json();
      }, 5000); // 5s cache after resolution
      }
      // Two simultaneous calls will share the same network request
      const [a, b] = await Promise.all([loadPatients(), loadPatients()]);