withLatestFrom
signature: withLatestFrom(other: Observable, project: Function): Observable
withLatestFrom(other: Observable, project: Function): Observable
Also provide the last value from another observable.
💡 If you want the last emission any time a variable number of observables emits, try combinelatest!
Why use withLatestFrom?
The withLatestFrom
operator is your best friend when you have one main observable whose emissions depend on the latest values from one or more other observables. Think of it as a one-way data flow, where the primary observable takes the lead and other observables chime in with their most recent values.
A classic example to remember withLatestFrom
is a chat application that needs to send a message with a user's current location. The message sending event (main observable) combines with the latest location data (another observable) to form the final message object.
Keep in mind that withLatestFrom
only emits a value when the main observable emits, and after each additional observable has emitted at least once. This can catch you off guard, as you might not see any output or errors while one of the observables isn't behaving as expected, or a subscription is delayed.
If you need to combine values from multiple observables that emit more than once and are interdependent, consider using combineLatest
instead. And for scenarios where observables emit only once or you just need their last values, forkJoin
might be a more suitable choice.
Examples
Example 1: Latest value from quicker second source
( StackBlitz | jsBin | jsFiddle )
Example 2: Slower second source
( StackBlitz | jsBin | jsFiddle )
Related Recipes
Additional Resources
withLatestFrom 📰 - Official docs
withLatestFrom - In Depth Dev Reference
Combination operator: withLatestFrom 🎥 💵 - André Staltz
📁 Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/withLatestFrom.ts
Last updated