title: Chain two retrofit observables w/ RxJava
body: <p>I want to execute 2 network calls one after another. Both network calls return Observable. Second call uses data from successful result of the first call, method in successful result of second call uses data from <strong>both</strong> successful result of the first and of the second call. Also i should be able to handle <strong>both</strong> onError "events" differently. How can i achieve this avoiding callback hell like in example below:</p>
  
  <pre><code>       API().auth(email, password)
              .subscribeOn(Schedulers.newThread())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(new Action1&lt;AuthResponse&gt;() {
                  @Override
                  public void call(final AuthResponse authResponse) {
                      API().getUser(authResponse.getAccessToken())
                              .subscribe(new Action1&lt;List&lt;User&gt;&gt;() {
                                  @Override
                                  public void call(List&lt;User&gt; users) {
                                      doSomething(authResponse, users);
                                  }
                              }, new Action1&lt;Throwable&gt;() {
                                  @Override
                                  public void call(Throwable throwable) {
                                      onErrorGetUser();
                                  }
                              });
                  }
              }, new Action1&lt;Throwable&gt;() {
                  @Override
                  public void call(Throwable throwable) {
                      onErrorAuth();
                  }
              });
  </code></pre>
  
  <p>I know about zip, but i want to <strong>avoid</strong> creating "Combiner class".</p>
  
  <p><strong>Update 1.</strong>
  Tried to implement akarnokd's answer:</p>
  
  <pre><code>         API()
              .auth(email, password)
              .subscribeOn(Schedulers.newThread())
              .observeOn(AndroidSchedulers.mainThread())
              .flatMap(authResponse -&gt; API()
                      .getUser(authResponse.getAccessToken())
                      .doOnError(throwable -&gt; {
                          getView().setError(processFail(throwable));
                      }), ((authResponse, users) -&gt; {
                  // Ensure returned user is the which was authenticated
                  if (authResponse.getUserId().equals(users.get(0).getId())) {
                      SessionManager.getInstance().initSession(email, password, authResponse.getAccessToken(), users.get(0));
                      getView().toNews();
                  } else {
                      getView().setError(R.string.something_went_wrong);
                  }
              }));
  </code></pre>
  
  <p>However inside <code>flatMap</code> method compiler says it can't resolve methods of authResponse and users (<code>authResponse.getAccessToken()</code>, <code>users.get(0)</code> etc). Im new to rx programming and lambdas - please tell me what's the problem. Anyway code looks much cleaner now.</p>
  
  <p><strong>Update 2.</strong></p>
  
  <pre><code>API()
              .auth(email, password)
              .subscribeOn(Schedulers.newThread())
              .observeOn(AndroidSchedulers.mainThread())
              .doOnError(throwable -&gt; getView().setError(processFail(throwable)))
              .flatMap((AuthResponse authResponse) -&gt; API()
                      .getUser(authResponse.getAccessToken())
                      .doOnError(throwable -&gt; getView().setError(processFail(throwable))), ((AuthResponse authResponse, List&lt;User&gt; users) -&gt; {
                              // Ensure returned user is the which was authenticated
                              if (authResponse.getUserId().equals(users.get(0).getId())) {
                                  SessionManager.getInstance().initSession(email, password, authResponse.getAccessToken(), users.get(0));
                                  getView().toNews();
                              }
                              return Observable.just(this);
              }));
  </code></pre>
  
  <p>Have done it like this, but now my network calls aren't executing at all.</p>