title: RxJava 2 - How to chain asyncronous calls
body: <p>I've just recently started learning RxJava so don't crucify me if I'm asking a newbie question but I've spend days now in trying to figure this out without any success. I've read almost all the documentation I could find and I followed along most of the tutorials on <a href="http://reactivex.io/tutorials.html" rel="nofollow noreferrer">http://reactivex.io/tutorials.html</a>. I searched StackOverflow and the rest of the Internet high and low but apparently I seem to be the only person on the planet with this problem. Which is odd since essentially it boils down to something every piece of software has to do: Logging in a user. </p>
  
  <p>All the tutorials I found are on applying some functions on a stream creating a new stream which is useful and awesome, don't get me wrong but not really helpful in my case. It also got me thinking ... maybe I go about this all wrong. But I'm stuck so deep now and also following the mantra that "everything is a stream" why should that not be possible?</p>
  
  <p>So here is what I'm trying to do:</p>
  
  <ol>
  <li>indicate some form of loading</li>
  <li>call a <code>Completable</code> to perform a login action on some server</li>
  <li>call a <code>Single</code> to perform a create user action on some server which returns a user id for local reference</li>
  <li>use the result of the <code>Single</code> call in the next action and hide loading</li>
  </ol>
  
  <p>Even though I will end up with this on Android I created a basic Java 8 example to outline what I would like to achieve.</p>
  
  <p>This is what I came up with so far:</p>
  
  <p>Notes:</p>
  
  <ul>
  <li>I'm using rxjava2</li>
  <li>the <code>getMainStream()</code> function is there to simulate some interaction</li>
  </ul>
  
  <p>Here a runnable version of the code:</p>
  
  <pre><code>public static void main(final String[] args) {
      getMainStream()
          .doOnNext(__ -&gt; showLoading())
          .flatMap(__ -&gt; loginUser().toObservable())
          .flatMap(__ -&gt; createUser().toObservable())
          .doOnNext(userId -&gt; {
              hideLoading();
              System.out.println("userId: " + userId);
          })
          .subscribe();
  }
  
  public static Completable loginUser() {
      return Completable.create(new CompletableOnSubscribe() {
          @Override
          public void subscribe(final CompletableEmitter e) throws Exception {
              Thread.sleep(500);
              System.out.println("loginUser");
              e.onComplete();
          }
      });
  }
  
  public static Single&lt;String&gt; createUser() {
      return Single.&lt;String&gt;create(new SingleOnSubscribe&lt;String&gt;() {
          @Override
          public void subscribe(final SingleEmitter&lt;String&gt; e) throws Exception {
              Thread.sleep(1000);
              System.out.println("createUser");
              e.onSuccess("some_user_id");
          }
      });
  }
  
  public static Completable getCompletable(final String input) {
      return Completable.create(new CompletableOnSubscribe() {
          @Override
          public void subscribe(final CompletableEmitter e) throws Exception {
              Thread.sleep(750);
              System.out.println("completable, input=" + input);
              e.onComplete();
          }
      });
  }
  
  public static Observable&lt;Object&gt; getMainStream() {
      return Observable.just(new Object());
  }
  
  private static void hideLoading() {
      System.out.println("hideLoading()");
  }
  
  private static void showLoading() {
      System.out.println("showLoading()");
  }
  </code></pre>
  
  <p>The console output of this is:</p>
  
  <pre><code>showLoading()
  loginUser
  </code></pre>
  
  <p>Unfortunately, the login user never returns?!</p>
  
  <p>I'm really looking forward to any help on this topic!</p>
  
  <p>Thanks!!!</p>