Documentation Index
Fetch the complete documentation index at: https://mintlify.com/ReactiveX/rxjs/llms.txt
Use this file to discover all available pages before exploring further.
Overview
ThedelayWhen operator delays each emission from the source Observable by a time span determined by another Observable. Unlike delay, which uses a fixed duration, delayWhen allows you to dynamically compute the delay for each value.
The source value is emitted only when the “duration” Observable emits a next value. Completion alone does not trigger emission.
Signature
Parameters
A function that returns an Observable (the “duration” Observable) for each value emitted by the source. The emission of that value is delayed until the duration Observable emits a next value.Parameters:
value: The emitted value from sourceindex: Zero-based emission index
Deprecated in v8. An Observable that triggers subscription to the source once it emits any value.
Returns
A function that returns an Observable that delays emissions based on the ObservableInput returned by
delayDurationSelector.Usage Examples
Random Delay
Delay each click by a random amount of time:Value-Based Delay
Delay based on the emitted value:Network Request Delay
Delay based on API response:Conditional Delays
Apply different delays based on conditions:How It Works
Important: In RxJS v7+, the duration Observable must emit a next value to trigger the delayed emission. Completion alone is not sufficient.
delayDurationSelectoris called with the value and index- The returned Observable becomes the “duration” Observable
- RxJS subscribes to the duration Observable
- When the duration Observable emits a next value:
- The source value is emitted to subscribers
- The duration Observable is unsubscribed
- If the duration Observable only completes (without nexting), the value is never emitted
- If the duration Observable errors, the error propagates to output
Important Behavior Changes
Common Use Cases
- Variable Rate Limiting: Different delays based on user role or subscription tier
- Adaptive Throttling: Delay based on server load or network conditions
- Priority Queues: Process high-priority items first with shorter delays
- Backoff Strategies: Implement exponential backoff for retries
- Coordinated Timing: Synchronize emissions with external events
Implementation Details
The operator is implemented usingmergeMap:
Performance Considerations
- Each emitted value creates a new subscription to a duration Observable
- For high-frequency sources, this can create many concurrent subscriptions
- Consider using
mergeMapwith concurrency limit if needed - Ensure duration Observables complete to avoid memory leaks
Common Patterns
Exponential Backoff
Stagger with Index
Related Operators
- delay - Fixed delay for all emissions
- debounce - Debounce with dynamic duration
- throttle - Throttle with dynamic duration
- mergeMap - Map to inner observables
- timer - Create delayed Observable
