toObserver
ThetoObserver 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
Either an Observer object with
next, error, and complete methods, or a simple callback function that receives the next value.Optional error handler function called when an error occurs. Only used when
nextHandler is a function.Optional completion handler called when the observable completes. Only used when
nextHandler is a function.Returns
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 callsubscribe() 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.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
Related APIs
- Observer - The observer interface type
- Subscription - The subscription object returned by subscribe
- createAtom - Uses toObserver internally for subscriptions