title: RxJS Observable.concat: How to know where next result came from?
body: <p>My application (Angular 2 with RxJS 5 written in TypeScript) requires me to make 2 calls to a web service in a predefined order. <a href="https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/concat.md" rel="nofollow">Inspired by this page</a> I decided to try the <code>concat</code> operator of RxJS observables. The twist is that the <code>onNext</code> callback that receives values needs to forward them to different handlers depending on where they came from</p>
  
  <p>My code can be simplified to:</p>
  
  <pre><code>suscription1 = Observable.return({name:'John', age:7});
  subscription2 = Observable.return({name:'Mary', color:'blue'}); 
  Observable.concat(subscription1, subscription2).subscribe(
      data=&gt;{ /* need to know which subscripion 
               i'm dealing with to direct to the right handler*/}
  );
  </code></pre>
  
  <p>What's the most elegant way to determine where my data came from at each observable emission? I have thought of a couple of ways but they all seem clumsy:</p>
  
  <ol>
  <li><p>Keep a counter in scope, perhaps as a class property. Check it and increment it each time you receive a result. Since each observable emits just once, the counter will have its initial value when the first subscription outputs and it will have initial+1 when the second outputs. Problem is this won't work when I don't know how many times each observable will emit before being complete.</p></li>
  <li><p>Inspect each result and identify its source by the result's shape. For instance, in my example only the first observable's result has an <code>age</code> property.  Obviously this becomes impractical when the results have the same shape.</p></li>
  <li><p>Get rid of the <code>concat</code> operator altogether; subscribe to <code>subscription2</code> in the <code>onComplete</code> handler of <code>subscription1</code>. This would work, but it doesn't scale well; if I have 4 or 5 subscriptions to concatenate, it becomes nested hell.  </p></li>
  </ol>
  
  <p>Solution 3 with just 2 subscriptions...</p>
  
  <pre><code>suscription1 = Observable.return({name:'John', age:7});
  subscription2 = Observable.return({name:'Mary', age:8}); 
  subscription1.subscribe(
      data=&gt; this.handlerOne(data),
      error=&gt;{},
      ()=&gt; {
          subscription2.subscribe(
              data=&gt;this.handlerTwo(data),
              error=&gt;{},
              ()=&gt;console.log('All subscriptions completed!')
          )
      }
  );
  </code></pre>
  
  <p>So my question: When using <code>Observable.concat</code> to subscribe to several observables in sequence, how can my <code>onNext</code> handler determine where the data came from? Alternatively, is there another operator I should be using for this purpose? I cannot use the <code>forkJoin</code> operator because the order in which the subscriptions are completed is important.</p>