It can happen quite often that there is a need to execute two asynchronous tasks and wait for the result of both of them before the next step can be taken.
For example, consider downloading data for two stocks to compare their prices. The fetches for both of them can be executed independently, so there is no need to wait for one of them to complete before starting another, but how do we do that?
.zip()
is an operation that combines items from two Observables into one by taking an item from each of the Observables and then producing a new value. It means that it waits for both of the Observables to produce a value before the flow continues.
This is useful in occasions where two values are produced independently but later (downstream), they are both consumed at the same time.
Let's see an example of that:
Observable.zip( Observable.just("One", "Two", "Three"), Observable.interval(1, TimeUnit.SECONDS), (number, interval) -> number + "-" + interval )...