Overview
TheobserveOn operator re-emits all notifications (next, error, and complete) from the source Observable using a specified scheduler. This allows you to control when and how notifications are delivered to observers.
Unlike
subscribeOn (which affects when subscription happens), observeOn affects when notifications are delivered to subscribers.Signature
Parameters
The scheduler that will be used to reschedule notifications from the source Observable. Common schedulers:
asyncScheduler- UsessetTimeoutasapScheduler- Uses microtasks (Promise.resolve)queueScheduler- Synchronous queueanimationFrameScheduler- UsesrequestAnimationFrame
Number of milliseconds to delay each notification. This is in addition to the scheduler’s natural timing.
Returns
A function that returns an Observable that emits the same notifications as the source, but rescheduled with the provided scheduler.
Usage Examples
Smooth Browser Animations
Ensure values are emitted just before browser repaint:Delayed Notifications
Add delay to all notifications including errors:Testing with Virtual Scheduler
Control timing in tests:Processing in Batches
Prevent blocking the main thread:How It Works
observeOn schedules each notification (next, error, complete) on the specified scheduler:
Difference: observeOn vs delay
| Feature | observeOn | delay |
|---|---|---|
| Delays next | ✅ | ✅ |
| Delays complete | ✅ | ❌ (immediate) |
| Delays error | ✅ | ❌ (immediate) |
| Purpose | Control scheduler | Add time delay |
| Use case | Execution context | Time shifting |
Common Use Cases
- Smooth Animations: Use
animationFrameSchedulerfor 60fps animations - UI Responsiveness: Prevent blocking with
asyncScheduler - Debouncing Work: Control when expensive operations run
- Testing: Use
TestSchedulerfor deterministic timing - Event Loop Control: Manage execution order and timing
Scheduler Comparison
| Scheduler | Timing | Use Case |
|---|---|---|
animationFrameScheduler | Before browser repaint (~16ms) | Animations, visual updates |
asapScheduler | Microtask queue (Promise) | Defer to next microtask |
asyncScheduler | Macrotask queue (setTimeout) | General async operations |
queueScheduler | Synchronous | Recursive operations, testing |
Performance Considerations
- Each notification creates a scheduled task
- For high-frequency sources (e.g., mouse move), consider
throttleTimeorauditTimefirst - Schedulers add overhead - use only when needed
- Animation frame scheduler is optimized for 60fps
Best Practices
- Place strategically: Put
observeOnwhere you need to change execution context - Combine with throttling: For high-frequency events, throttle before observeOn
- Use appropriate scheduler: Match scheduler to use case
- Avoid overuse: Only reschedule when necessary
- Consider delay operator: If you only need time delay (not scheduler control), use
delay
Related Operators
- subscribeOn - Control subscription scheduler
- delay - Delay values (but not errors)
- throttleTime - Limit emission rate
- debounceTime - Delay after silence
Related Schedulers
- asyncScheduler - setTimeout-based
- asapScheduler - Microtask-based
- queueScheduler - Synchronous queue
- animationFrameScheduler - requestAnimationFrame
