title: Nested Subscriptions with RxJava Timers and Timeouts
body: <p>Using RxJava2, I am trying to accomplish some timed events.</p>
  
  <p>I have a few async events that might complete in less than one second, and I want to be displaying a message to the user for at least one second, before continuing on with the result of the async operation (and maybe use that result later).</p>
  
  <p>The async operation may also timeout, and I would want to display a message and not continue.</p>
  
  <p>I can accomplish this by using an <code>zip()</code> with the timer as the first parameter and the async operation as the second operator, but what do I do with the next 'layer'?</p>
  
  <p>This is the code I have so far, which actually <strong><em>does work</em></strong>, but I feel very dirty creating nested subscriptions (using <code>just()</code> in place of the async operation, and ignoring subscription threads)</p>
  
  <p>mStrings is just a <code>BehaviorSubject&lt;String&gt;</code>.</p>
  
  <pre><code>mStrings.onNext("Waiting 1 second. The result will fire at the Single.timer onComplete");
  Single.zip(Single.timer(1, TimeUnit.SECONDS), Single.just("First"), (t1, t2) -&gt; t2)
    .timeout(15, TimeUnit.SECONDS, observer -&gt; {
        mStrings.onNext("First timeout fired");
    })
    .subscribe(s1 -&gt; {
        mStrings.onNext("First timer fired and returned " + s1);
        Single.zip(Single.timer(1, TimeUnit.SECONDS), Single.just("Second"), (t1, t2) -&gt; t2)
          .timeout(15, TimeUnit.SECONDS, observer -&gt; {
              mStrings.onNext("Second timeout fired");
          })
          .subscribe(s2 -&gt; {
              mStrings.onNext("Second timer fired and returned " + s2 + ". Previous was " + s1);
              Single.zip(Single.timer(1, TimeUnit.SECONDS), Single.just("Third"), (t1, t2) -&gt; t2)
                .timeout(15, TimeUnit.SECONDS, observer -&gt; {
                    mStrings.onNext("Third timeout fired");
                })
                .subscribe(s3 -&gt; {
                    mStrings.onNext("Third timer fired and returned " + s3 + ". Previous was " + s1 + " and " + s2 );
                });
          });
    });
  </code></pre>
  
  <p>The result of which is:</p>
  
  <pre><code>17:53:53.219 Waiting 1 second. The result will fire at the Single.timer onComplete
  17:53:54.220 First timer fired and returned First
  17:53:55.224 Second timer fired and returned Second. Previous was First
  17:53:56.224 Third timer fired and returned Third. Previous was First and Second
  </code></pre>
  
  <p>Am I missing an operator that would make sense in this type of flow? Or some elementary methodology? I know I might be able to work out an alternative solution using multiple subjects, but it seems excessive.</p>