Chaining Observables in RxJS
<p>I'm learning RxJS and Angular 2. Let's say I have a promise chain with multiple async function calls which depend on the previous one's result which looks like: </p>

<pre><code>var promiseChain = new Promise((resolve, reject) =&gt; {
  setTimeout(() =&gt; {
    resolve(1);
  }, 1000);
}).then((result) =&gt; {
  console.log(result);

  return new Promise((resolve, reject) =&gt; {
    setTimeout(() =&gt; {
      resolve(result + 2);
    }, 1000);
  });
}).then((result) =&gt; {
  console.log(result);

  return new Promise((resolve, reject) =&gt; {
      setTimeout(() =&gt; {
      resolve(result + 3);
        }, 1000);
  });
});

promiseChain.then((finalResult) =&gt; {
  console.log(finalResult);
});
</code></pre>

<p>My attempts at doing the same solely using RxJS without the use of promises produced the following:</p>

<pre><code>var observableChain = Observable.create((observer) =&gt; {
  setTimeout(() =&gt; {
    observer.next(1);
    observer.complete();
  }, 1000);
}).flatMap((result) =&gt; {
  console.log(result);

  return Observable.create((observer) =&gt; {
    setTimeout(() =&gt; {
      observer.next(result + 2);
      observer.complete()
    }, 1000);
  });
}).flatMap((result) =&gt; {
  console.log(result);

  return Observable.create((observer) =&gt; {
    setTimeout(() =&gt; {
      observer.next(result + 3);
      observer.complete()
    }, 1000);
  });
});

observableChain.subscribe((finalResult) =&gt; {
  console.log(finalResult);
});
</code></pre>

<p>It yields the same output as the promise chain. My questions are</p>

<ol>
<li><p>Am I doing this right? Are there any RxJS related improvements that I can make to the above code</p></li>
<li><p>How do I get this observable chain to execute repeatedly? i.e. Adding another subscription at the end just produces an additional 6 though I expect it to print 1, 3 and 6.</p>

<p>observableChain.subscribe((finalResult) => {
  console.log(finalResult);
});</p>

<p>observableChain.subscribe((finalResult) => {
  console.log(finalResult);
});</p>

<p>1
3
6
6</p></li>
</ol>