Search code examples
javarx-javareactive-programmingreactivex

Concat Observables of different types


I have a network call that authenticates and receives a token and a second one that, using this token, requests data.

I want to combine those into a Single<Data>. In order to do so I was looking at concat, but this seems to only work for Observable of the same type. Using zip I could return just the response of the second Observable, but to my understanding it works in paralell while I need to wait for the first Observable and handle its response before I can start the second one.

What would be the operator I should use for this case?


Solution

  • Since request token and request data are dependent steps then use flatMap:

    Observable<String> getToken(String user, String password) {
       ...
    }
    
    Single<Data> getData(String token) {
       ...
    }
    

    Usage:

    Single<Data> data = getToken(user, pass).flatMap(token -> getData(token))