delay
signature: delay(delay: number | Date, scheduler: Scheduler): Observable
delay(delay: number | Date, scheduler: Scheduler): Observable
Delay emitted values by given time.
Why use delay
?
delay
?This operator is your go-to when simulating real-world scenarios such as network latency or introducing a pause before a value is emitted. The delay
operator allows you to hold back values for a specified duration before they're released to subscribers.
Keep in mind that delay
won’t prevent the original observable from emitting values. It merely postpones the delivery to its subscribers. This is a gotcha as it could look like your data is lagging or not in sync with the source, especially when multiple observables are at play.
Examples
Example 1: Delay to recognize long press
( StackBlitz )
Example 2: Delay for increasing durations
( StackBlitz )
Related Recipes
Additional Resources
delay 📰 - Official docs
delay - In Depth Dev Reference
Transformation operator: delay and delayWhen 🎥 💵 - André Staltz
📁 Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/delay.ts
Last updated