Chaining Singles together in RxJava : <p>I am using RxJava in my android app along with Retrofit to make network requests to a server. I am using RxJavaCallAdapterFactory so I can have my retrofit requests return singles. In my code, the retrofit object is named 'api'.</p>

<p>The code here works fine, but in this example, I need to retrieve the userId before I can make a playlist. I flat map the userId request to the API request, and after making the playlist, I need to use flat map again to convert the JSON response to a usable object. </p>

<pre><code>public JSONUser me;

public Single&lt;String&gt; getUserId(){

    if(me != null){
        return Single.just(me.getUserId());
    }

    return api.getMe().flatMap(new Func1&lt;JSONUser, Single&lt;String&gt;&gt;() {
        @Override
        public Single&lt;String&gt; call(JSONUser meResult) {
            me = meResult;
            return Single.just(me.getUserId());
        }
    });
}

public Single&lt;Playlist&gt; createPlaylist(String name) {

    final NewPlaylistConfig config = new NewPlaylistConfig(name);

    return getUserId().flatMap(new Func1&lt;String, Single&lt;Playlist&gt;&gt;() {
        @Override
        public Single&lt;Playlist&gt; call(String userId) {
            return api.createPlaylist(userId, config).flatMap(
                    new Func1&lt;JSONPlaylist, Single&lt;? extends SpotifyPlaylist&gt;&gt;() {
                        @Override
                        public Single&lt;? extends Playlist&gt; call(JSONPlaylist data) {
                            return Single.just(new Playlist(data));
                    }
            });
        }
    });
}
</code></pre>

<p>The entry point here would be createPlaylist(). NewPlaylistConfig will be converted to JSON and is simply the body parameter for the POST request. UserId is needed as a path parameter. </p>

<p>My main question here, is if there is a way to chain these operations without the "callback-hell" you see here. Like I said, this code works but it is really ugly. I would appreciate if somebody could point me in the right direction regarding this. Is there a way to make this work like promises where you can just chain .thens?</p>

<p>Thank you.</p>
