Skip to main content

TimeoutManager

Provides a customizable interface for managing timeouts and intervals in TanStack Query. This utility allows you to replace the default global setTimeout and setInterval implementations with custom providers, which is useful for testing, optimization, or alternative timer implementations.

Instance

A singleton instance is exported as timeoutManager:

Why TimeoutManager?

TanStack Query makes extensive use of timeouts to implement features like staleTime and gcTime. The default implementation uses the platform’s global setTimeout, which can have scalability issues with thousands of timeouts on the event loop. The TimeoutManager provides a way to:
  • Replace timeout implementation for better performance
  • Mock timers in tests
  • Implement timeout coalescing for better scalability
  • Use alternative timer implementations (e.g., setImmediate, custom schedulers)

Methods

setTimeout

Schedules a callback to be executed after a specified delay.
(_: void) => void
required
The function to execute after the delay. The callback accepts a single void argument to maintain compatibility with platform setTimeout typings.
number
required
The delay in milliseconds before executing the callback.
Returns: A timer ID that can be used with clearTimeout to cancel the timeout. The type is ManagedTimerId which can be a number or an object with a numeric value. Usage:

clearTimeout

Cancels a timeout that was previously scheduled with setTimeout.
ManagedTimerId | undefined
The timer ID returned from setTimeout. Can be undefined for convenience (will be a no-op).
Usage:

setInterval

Schedules a callback to be executed repeatedly at a specified interval.
(_: void) => void
required
The function to execute at each interval. The callback accepts a single void argument to maintain compatibility with platform setInterval typings.
number
required
The delay in milliseconds between each execution of the callback.
Returns: A timer ID that can be used with clearInterval to cancel the interval. Usage:

clearInterval

Cancels an interval that was previously scheduled with setInterval.
ManagedTimerId | undefined
The timer ID returned from setInterval. Can be undefined for convenience (will be a no-op).
Usage:

Advanced Configuration

setTimeoutProvider

Replaces the default timeout provider with a custom implementation.
TimeoutProvider<TTimerId>
required
An object implementing the TimeoutProvider interface with setTimeout, clearTimeout, setInterval, and clearInterval methods.Type definition:
Usage:
Warning: Changing the provider after timeouts have been created may result in unexpected behavior, as clearTimeout calls may not work correctly with timer IDs from the previous provider. In development mode, a warning is logged if you switch providers after timers have been created.

Complete Examples

Example 1: Mock timers for testing

Example 2: Coalescing timeouts for better performance

Example 3: Using in TanStack Query

Default Implementation

By default, the TimeoutManager uses the platform’s global timer functions:

Type Definitions

Notes

  • The TimeoutCallback type requires a single void argument for compatibility with Promise constructors and platform typings
  • Timer IDs from different providers may not be compatible - avoid switching providers after creating timers
  • The TimeoutManager is used internally throughout TanStack Query for staleTime, gcTime, retry delays, and other timing features
  • Custom providers must implement all four methods: setTimeout, clearTimeout, setInterval, clearInterval

See Also