title: How would I write this code with Reactive Programming?
body: <p>I just started messing around with reactive programming, and I know just enough to write code but not enough to figure out what's happening when I don't get what I expect. I don't really have a mentor available other than blog posts. I haven't found a very good solution to a situation I'm having, and I'm curious about the right approach.</p>
  
  <p>The problem:</p>
  
  <p>I need to get a Foo, which is partially composed of an array of Bar objects. I fetch the Bar objects from web services. So I represented each web service call as an IObservable from which I expect 0 or 1 elements before completion. I want to make an IObservable that will:</p>
  
  <ul>
  <li>Subscribe to each of the IObservable instances.</li>
  <li>Wait for up to a 2 second Timeout.</li>
  <li>When either both sequences complete or the timeout happens:
  
  <ul>
  <li>Create an array with any of the Bar objects that were generated (there may be 0.)</li>
  <li>Produce the Foo object using that Bar[]. </li>
  </ul></li>
  </ul>
  
  <p>I sort of accomplished this with this bit of code:</p>
  
  <pre><code>public Foo CreateFoo() {
  
          var producer1 = webService.BarGenerator()
                                    .Timeout(TimeSpan.FromSeconds(2), Observable.Empty&lt;Bar&gt;());
          var producer2 = // similar to above
  
          var pipe = producer1.Concat(producer2);
  
          Bar[] result = pipe.ToEnumerable().ToArray();
  
          ...
  }
  </code></pre>
  
  <p>That doesn't seem right, for a lot of reasons. The most obvious is Concat() will start the sequences serially rather than in parallel, so that's a 4-second timeout. I don't really care that it blocks, it's actually convenient for the architecture I'm working with that it does. I'm fine with this method becoming a generator of IObservable, but there's a few extra caveats here that seem to make that challenging when I try:</p>
  
  <ol>
  <li>I need the final array to put producer1 and producer2's result in that order, if they both produce a result.</li>
  <li>I'd like to use a TestScheduler to verify the timeout but haven't succeeded at that yet, I apparently don't understand schedulers at all.</li>
  <li>This is, ultimately, a pull model, whatever gets the Foo needs it at a distinct point and there's no value to receiving it 'on the fly'. Maybe this tilts the answer to "Don't use Rx". To be honest, I got stuck enough I switched to a Task-based API. But I want to see how one might approach this with Rx, because I want to learn.</li>
  </ol>