# Function withTimeoutAndAbort

- withTimeoutAndAbort<T>(
  fn: (signal: AbortSignal) => Promise<T>,
  timeoutMs: number,
  errorMessage?: string,
  ): Promise<T>

  Wraps a promise with a timeout and optional AbortSignal support.

  For operations that support cancellation (like fetch), pass a function that accepts an AbortSignal. The signal will be aborted when the timeout fires, allowing the underlying operation to clean up resources.

  #### Type Parameters

  - T

    Type of the promise result

  #### Parameters

  - fn: (signal: AbortSignal) => Promise<T>

    Function that returns a promise and optionally accepts an AbortSignal

  - timeoutMs: number

    Timeout in milliseconds

  - `Optional` errorMessage: string

    Custom error message (optional)

  #### Returns Promise<T>

  Promise that resolves/rejects with the operation result or timeout

  #### Throws

  If the operation exceeds `timeoutMs`

  #### Example

      // With AbortSignal support (for fetch, etc.)
      await withTimeoutAndAbort(
        (signal) => fetch(url, { signal }),
        5000,
        'API request timed out'
      );

  #### Security

  Aborts the underlying operation via `AbortController` when the timeout fires, preventing dangling fetch connections and resource leaks. Per ISMS Policy SC-002, all external network calls must be cancellable.

  #### Since

  0.8.0

  - Defined in utils/timeout.ts:151