How can I create an eager observable from a hot observable?

717 Views Asked by At

AFAIK, observable is lazy.

import * as rxjs from 'rxjs'
const { filter, take, map } = rxjs.operators

function awesomeOpators() {
  return take(1);
}

const numbers$ = new rxjs.Subject<number>();
const start$ = numbers$.pipe(
  awesomeOpators(),
);


numbers$.next(1);

start$.subscribe((val) => {
  // outputs 2
  console.log(val)
})

numbers$.next(2)

How can I rewrite awesomeOpators such that beginWithLargeNumbe$ starts with 1?
https://stackblitz.com/edit/rxjs-zqkz7r?file=main.ts

1

There are 1 best solutions below

3
Ingo Bürk On

You can use a ReplaySubject instead:

const numbers$ = new rxjs.ReplaySubject<number>();

There's a bunch of other ways as well. You haven't told us which part you can or can't control, though, so this will do.