merge
signature: merge(input: Observable): Observable
merge(input: Observable): Observable
Turn multiple observables into a single observable.
💡 This operator can be used as either a static or instance method!
💡 If order not throughput is a primary concern, try concat instead!
Why use merge?
The merge operator is your go-to solution when you have multiple observables that produce values independently and you want to combine their output into a single stream. Think of it as a highway merger, where multiple roads join together to form a single, unified road - the traffic (data) from each road (observable) flows seamlessly together.
A real-world example can be seen in a chat application, where you have separate observables for receiving messages from multiple users. By using merge
, you can bring all those message streams into a single unified stream for displaying the messages in the chat window.
Keep in mind that merge
will emit values as soon as any of the observables emit a value. This is different from combineLatest or withLatestFrom, which wait for each observable to emit at least one value before emitting a combined value.
Lastly, if you're dealing with observables that emit values at specific intervals and you need to combine them based on time, consider using the zip operator instead.
Examples
Example 1: merging multiple observables, static method
( StackBlitz | jsBin | jsFiddle )
Example 2: merge 2 observables, instance method
( StackBlitz | jsBin | jsFiddle )
Related Recipes
Additional Resources
merge 📰 - Official docs
merge - In Depth Dev Reference
Handling multiple streams with merge 🎥 💵 - John Linquist
Sharing network requests with merge 🎥 💵 - André Staltz
Combination operator: merge 🎥 💵 - André Staltz
Build your own merge operator 🎥 - Kwinten Pisman
📁 Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/merge.ts
Last updated