3

So what is the difference between of operator and Observable.of and what is the recommended way to create an observable?

import { Observable } from 'rxjs/Observable';
const obs$ = Observable.of(3);

or

import { of as observableOf } from 'rxjs';
const obs$ = observableOf(3);
Rachid O
  • 13,013
  • 15
  • 66
  • 92
  • What is the relation between Operator and Observable? – Ramesh Rajendran May 24 '18 at 11:02
  • @RameshRajendran I'm asking the difference between Observable.of and 'of' not between Observable and of operator – Rachid O May 24 '18 at 11:04
  • 4
    If you import `Observable`, you can use all global operators right away. Your second snippet requires modification of `import` every time you need to use another operator. IIRC, before RxJS 6 it was recommended way because tree shaking was broken, but in 6+ you can do what you did in the first snippet. Also, second form is more "functional", as you can compose it using `pipe` operator, but first form is closer to JS way. – alx May 24 '18 at 11:05
  • @alx _" but in 6+ you can do what you did in the first snippet. "_ No, you can't. _" first form is closer to JS way"_ Why not the second one? – a better oliver May 24 '18 at 11:48
  • https://stackoverflow.com/questions/50218784/rxjs-6-how-to-get-observable-of-to-work – martin May 24 '18 at 12:01

1 Answers1

6

The second way is the preferred way, and going forward (from rxjs v7) the only way. Currenly in v6 you can still use the first by including the rxjs-compat package, but writing new code, you should stick to the last method mentioned.

Per Hornshøj-Schierbeck
  • 15,097
  • 21
  • 80
  • 101