Skip to main content

toObserver

The toObserver utility function converts different callback formats into a standardized Observer object. This is primarily used internally but is exported for advanced use cases where you need to work with the observer pattern directly.

Signature

Parameters

Observer<T> | ((value: T) => void)
Either an Observer object with next, error, and complete methods, or a simple callback function that receives the next value.
(error: any) => void
Optional error handler function called when an error occurs. Only used when nextHandler is a function.
() => void
Optional completion handler called when the observable completes. Only used when nextHandler is a function.

Returns

Observer<T>
A standardized Observer object with optional next, error, and complete methods.

Usage

Convert function to observer

Convert with error and completion handlers

Pass through existing observer

Internal usage in subscriptions

This function is used internally when you call subscribe() on stores and atoms to normalize different callback formats:

When to Use

Most applications won’t need to use toObserver directly. It’s primarily an internal utility.
You might use toObserver if you’re:
  • Building custom reactive primitives that follow the observer pattern
  • Creating wrapper libraries around TanStack Store
  • Implementing advanced subscription management systems
  • Working with RxJS or other observable libraries and need format conversion
  • Observer - The observer interface type
  • Subscription - The subscription object returned by subscribe
  • createAtom - Uses toObserver internally for subscriptions