title: RxJs: Execute a long running function immediately after returning, giving the caller an Observable for updates
body: <p>I have a function that could be considered long running (actually, it's multi-step where each step could be waiting for an external event like a response from a HTTP call).</p>
  <p>There's an invoker function to this which should return an observable that returns the updates to the original function. The original function MUST run whether or not the returned observable is subscribed to. Basically, it needs to return a hot observable.</p>
  <p>I have tried the following approach but, cannot get it to work:</p>
  <pre class="lang-js prettyprint-override"><code>function longRunningOperation(): Observable&lt;number&gt; {
    const updates$ = new Subject&lt;number&gt;();
    Promise.resolve().then(() =&gt; {
      console.log('starting updates...');
      updates$.next(1);
      updates$.next(2);
      updates$.next(3);
      updates$.complete();
    });
    return updates$;
  }
  </code></pre>
  <p>If I do a marble test on the above, I see that the actual events generated being empty (though the function does execute).</p>
  <pre class="lang-js prettyprint-override"><code>  it('should return and start executing', () =&gt; {
      const updates$ = longRunningOperation();
      const marbles = '(abc|)';
      const events = { a: 1, b: 2, c: 3 };
  
      new TestScheduler((actual, expected) =&gt;
        expect(actual).toEqual(expected)
      ).run(({ expectObservable }) =&gt; {
        expectObservable(updates$).toBe(marbles, events);
        console.log('Test Scheduler subscribed');
      });
    });
  </code></pre>
  <p>What am I doing wrong here?</p>
  <p>Link to demo
  <a href="https://stackblitz.com/edit/jasmine-in-angular-upoavr?file=src/app/app.component.spec.ts" rel="nofollow noreferrer">https://stackblitz.com/edit/jasmine-in-angular-upoavr?file=src/app/app.component.spec.ts</a></p>