title: RxJs: How to loop based on state of the observable?
body: <p>I'm trying to get RxJs to loop over an Observable in my stream until it is in a certain state, then have the stream continue. Specifically I'm converting a synchronous do/while loop to RxJs, but I assume the same answer could be used for a for or while loop as well.</p>
  
  <p>I thought I could use doWhile() for this, but it seems like the condition function does not have access to the item in the stream, which seems to defeat the purpose to me.</p>
  
  <p>I'm not completely sure what the correct reactive terminology is for what I want, but here is an example of what I am going for:</p>
  
  <pre class="lang-js prettyprint-override"><code>var source = new Rx.Observable.of({val: 0, counter: 3});
  
  source.map(o =&gt; {
    o.counter--;
    console.log('Counter: ' + o.counter);
  
    if (!o.counter) {
      o.val = "YESS!";
    }
    return o;
  })
  .doWhile(o =&gt; { 
    return o.counter &gt; 0; 
  })
  .subscribe(
      function (x) {
          console.log('Next: ' + x.val);
      },
      function (err) {
          console.log('Error: ' + err);   
      },
      function () {
          console.log('Completed');   
      });
  </code></pre>
  
  
  
  <p>The expected output would be:</p>
  
  <pre><code>Counter: 3
  Counter: 2
  Counter: 1
  Counter: 0
  Next: YESS!
  Completed
  </code></pre>
  
  <p>Assuming this is a solvable problem, I am unclear on how you mark the 'start' of where you want to return when you loop.</p>