# Function withTimeout

- withTimeout<T>(
  promise: Promise<T>,
  timeoutMs: number,
  errorMessage?: string,
  ): Promise<T>

  Execute a promise with a timeout

  Races the provided promise against a timeout. If the timeout expires before the promise resolves, a TimeoutError is thrown.

  #### Type Parameters

  - T

    Type of the promise result

  #### Parameters

  - promise: Promise<T>

    Promise to execute with timeout

  - timeoutMs: number

    Timeout in milliseconds

  - `Optional` errorMessage: string

    Optional custom error message

  #### Returns Promise<T>

  Promise that resolves with the result or rejects with TimeoutError

  #### Throws

  If operation exceeds timeout

  #### Example

      const result = await withTimeout(
        fetchFromAPI('/endpoint'),
        5000,
        'API request timed out'
      );

  #### Security

  - Prevents resource exhaustion from hanging operations
  - Ensures responsive API behavior
  - Timeout values should be tuned per operation

  #### Since

  0.8.0

  - Defined in utils/timeout.ts:95