Skip to content

Commit db149ef

Browse files
committed
3.4 Added mergeWith
1 parent 5dc11cc commit db149ef

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Part 3 - Taming the sequence/4. Combining sequences.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,15 @@ First
323323

324324
The difference between `concat` and `merge` is that `merge` does not wait for the current observable to terminate before moving to the next. `merge` subscribes to every observable available to it and emits items as they come. It that way, `merge` is similar to the flattening part of `flatMap`.
325325

326+
Like other static operators, `merge` has an alternative that allows you to merge sequences one by one in a chain. The operator is called `mergeWith` and the behaviour is the same. The following example has the same result as the one above.
327+
328+
```java
329+
Observable.interval(250, TimeUnit.MILLISECONDS).map(i -> "First")
330+
.mergeWith(Observable.interval(150, TimeUnit.MILLISECONDS).map(i -> "Second"))
331+
.take(10)
332+
.subscribe(System.out::println);
333+
```
334+
326335
### mergeDelayError
327336

328337
With `merge`, as soon as any of the source sequences fails, the merged sequence fails as well. An alternative to that behaviour is `mergeDelayError`, which will postpone the emission of an error and continue to merge values from sequences that haven't failed.

0 commit comments

Comments
 (0)