title: How to replace 'if statement' with rx-java for avoiding callback hell?
body: <p>I am trying to replace my code with rx-java. (It is very small code.)</p>
  
  <p>It is finished and it works.</p>
  
  <p>But I want to know...</p>
  
  <ol>
  <li>Is it a good Rx style?</li>
  <li>If not good, please indicate bad point</li>
  </ol>
  
  <p>Below is my code that is api handling.</p>
  
  <p>before</p>
  
  <pre><code>Random r = new Random();
  boolean apiResult = r.nextBoolean(); // it represents api result. ex. {"result": true} or {"result": false}
  
  if (apiResult == true) {
      // do something
  
      System.out.println("result:" + "success");
  } else {
      // do something
  
      System.out.println("result:" + "failure");
  }
  </code></pre>
  
  <p>after</p>
  
  <pre><code>Random r = new Random();
  Observable&lt;Boolean&gt; apiResultStream = Observable.create(new OnSubscribe&lt;Boolean&gt;() {
      @Override
      public void call(Subscriber&lt;? super Boolean&gt; subscriber) {
          // emit true or false
           subscriber.onNext(r.nextBoolean());
      }
  }).cache(1);
  
  
  // I used filter for split. Is it Rx style?
  // success if true emitted.
  Observable&lt;Boolean&gt; successStream = apiResultStream
          .filter(aBoolean -&gt; aBoolean == true); // here
  
  // failure if false emitted.
  Observable&lt;Boolean&gt; failureStream = apiResultStream
          .filter(aBoolean -&gt; aBoolean == false); // here
  
  
  // success flow
  successStream
          .flatMap(aBoolean -&gt; Observable.just("success"))
          // and do something
          .subscribe(aString -&gt; System.out.println("result:" + aString));
  
  // failure flow
  failureStream
          .flatMap(aBoolean -&gt; Observable.just("failure"))
          // and do something.
          // I want to keep subscriber.
          .subscribe(aString -&gt; System.out.println("result:" + aString));
  </code></pre>
  
  <p><strong>EDIT</strong> </p>
  
  <p>I almost replaced. <a href="https://stackoverflow.com/questions/28290163/how-to-replace-if-statement-in-rx-java/28323856#28323856">thanks for good comment.</a><br>
  (but I have a few non-replaced code. It have many callback and if statement.)</p>
  
  <p>I want to avoid 'callback hell'.</p>
  
  <p>The key is different result type between 'callSuccessApi' and 'callFailureApi'</p>
  
  <p>before rx</p>
  
  <pre><code>// callback hell!
  callApi(new Callback&lt;Result&gt;(){
      @Override
      public void success(Result result) {
          if (result.Response == true) {
              callSuccessApi(new Callback&lt;ResultSuccess&gt;(){
                  @Override
                  public void success(ResultSuccess result) {
                      // and more callbacks...
                  }
              }
          } else { // result.Response == false
              callFailureApi(new Callback&lt;ResultFailure&gt;(){
                  @Override
                  public void success(ResultFailure result) {
                      // and more callbacks...
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>after with rx(avoid callback hell! Is it a good Rx style?)</p>
  
  <pre><code>// change 1st api to observable.(I changed other api to observable)
  Observable&lt;Result&gt; apiResultStream = Observable.create(new OnSubscribe&lt;Boolean&gt;() {
      @Override
      public void call(Subscriber&lt;? super Boolean&gt; subscriber) {
          callApi(new Callback&lt;Result&gt;(){
              @Override
              public void success(Result result) {
                  subscriber.onNext(result);
              }
          });
      }
  }).cache(1); // ensure same Observable&lt;Result&gt; for success and failure.
  
  
  // I used filter for split. Is it Rx style?
  // success if result.response == true.
  Observable&lt;ResultSuccess&gt; successStream = apiResultStream
          .filter(result -&gt; result.response == true); // here
  
  // failure if result.response == false.
  Observable&lt;ResultFailure&gt; failureStream = apiResultStream
          .filter(result -&gt; result.response == false); // here
  
  
  // success flow. callSuccessApi return Observable&lt;ResultSuccess&gt;
  successStream
          .flatMap(result -&gt; callSuccessApi(result))
          // and more api call with flatMap...
          .subscribe(resultSuccessN -&gt; System.out.println("result:" + resultSuccessN.toString()));
  
  // failure flow. callFailureApi return Observable&lt;ResultFailure&gt;
  failureStream
  .flatMap(resultFailure -&gt; callFailureApi(result))
          // and more api call with flatMap...
          .subscribe(resultFailureN -&gt; System.out.println("result:" + resultFailureN.toString()));
  </code></pre>
  
  <p>sorry for my poor English and long question.</p>
  
  <p><strong>Updated My Code</strong></p>
  
  <p>I got 2 important information in this question.(thank you @Tomáš Dvořák, @Will</p>
  
  <ol>
  <li>whether it is a good way to go depends on the particular situation. </li>
  <li>There's nothing wrong with using an if statement within an map / flatmap / subscribe.</li>
  </ol>
  
  <p>updated code</p>
  
  <pre><code>Observable&lt;Result&gt; apiResultStream = Observable.create(new OnSubscribe&lt;Boolean&gt;() {
          @Override
          public void call(Subscriber&lt;? super Boolean&gt; subscriber) {
              callApi(new Callback&lt;Result&gt;() {
                  @Override
                  public void success(Result result) {
                      subscriber.onNext(result);
                  }
              });
          }
      });
  
      // In this case,   I used 'if' for simply and cleanly.
      apiResultStream
              .subscribe(result -&gt; {
                  if (result.response == true) {
                      callSuccessApi(); // this line looks like 'callback'. but I used this for simply and cleanly.
                  } else {
                      callFailureApi();
                  }
              });
  </code></pre>