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

    Function wrapWithBreaker

    • Wraps an async operation with a circuit breaker.

      Type Parameters

      • T

      Parameters

      • key: string

        Unique key for the breaker instance (e.g., "GET:/api/patients").

      • fn: () => Promise<T>

        Function returning a Promise to protect.

      • cfg: Partial<BreakerCfg> = {}

        Partial breaker configuration (optional).

      Returns Promise<T>

      The resolved value from the operation, or throws if the breaker is open.

      // Protect a fetch call with a circuit breaker
      const result = await wrapWithBreaker('GET:/api/patients', () => fetch('/api/patients'));
      // Custom thresholds
      const data = await wrapWithBreaker('POST:/api/orders', () => postOrder(), { failureThreshold: 3, cooldownMs: 5000 });