RxJs: poll until interval done or correct data received : <p>How do i execute the following scenario in the browser with RxJs:</p>

<ul>
<li>submit data to queue for processing</li>
<li>get back the job id</li>
<li>poll another endpoint every 1s until result is available or 60seconds have passed(then fail)</li>
</ul>

<p>Intermediate solution that i've come up with:</p>

<pre><code> Rx.Observable
    .fromPromise(submitJobToQueue(jobData))
    .flatMap(jobQueueData =&gt; 
      Rx.Observable
            .interval(1000)
            .delay(5000)
            .map(_ =&gt; jobQueueData.jobId)
            .take(55)
    )
    .flatMap(jobId =&gt; Rx.Observable.fromPromise(pollQueueForResult(jobId)))
    .filter(result =&gt; result.completed)
    .subscribe(
      result =&gt; console.log('Result', result),
      error =&gt;  console.log('Error', error)
    );
</code></pre>

<ol>
<li>Is there a way without intermediate variables to stop the timer once the data arrives or error occurs? I now i could introduce new observable and then use <code>takeUntil</code></li>
<li>Is <code>flatMap</code> usage here semantically correct? Maybe this whole thing should be rewritten and not chained with <code>flatMap</code> ?</li>
</ol>
