33

I'm updating our software substituting all promises (and other hairy junk) for observables. To make sure that I'm following best practices, I made a quick googlearch and noticed that in some cases, the suggested syntax is by instance whereas in other cases, the examples perform a call by factory.

const byInstance = new Observable(_ => { ... });

const byFactory = Rx.Observable.create(_ => { ... });

I'm curious what's the actual difference. Are they precisely interchangeable? Is it an older/newer syntax/approach? Is it framework related? And, of course, which is to be preferred (under the condition that it's not opinionated, disputed etc.).

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • In the current version, there is no difference. The factory method [just calls the constructor](https://github.com/ReactiveX/rxjs/blob/5.4.3/src/Observable.ts#L56-L58). As to which is preferred, the [doc](http://reactivex.io/rxjs/manual/overview.html#creating-observables) suggests the factory, but whether or not that's a definitive answer: who knows? Not me. – cartant Aug 28 '17 at 06:32
  • @cartant I've performed an **immensely** quick and **highly** unscientific inquiry to find any indications whether there's a trend on who/when/why uses the two alternatives. It seems that the more Angular or .NET background, the more likely to see `new Observable` syntax. I can't vouch for any of it but still... – Konrad Viltersten Aug 28 '17 at 06:44
  • I think the accepted answer makes a good point - it got my vote - re: chaining and usage elsewhere. Anyway, I use the factory method, but try to avoid creating custom observables in the first place. – cartant Aug 28 '17 at 06:47

2 Answers2

28

There is no difference. Observable.create calls new Observable.

As the manual says,

Observables are created using Rx.Observable.create or a creation operator

<...>

Rx.Observable.create is an alias for the Observable constructor

Observable.create is conventionally used, probably because it reads better in chains and conforms with other Observable static methods that create observables, too.

The difference may appear in child classes. For example, Subject.create is equal to AnonymousSubject.create and not equal to new Subject. Usually Subject.create is the one that provides desirable behavour, while new Subject is more low-level. This confirms the point about the convention.

On the other hand, some classes (notably BehaviorSubject) are supposed to be used with new because create signature doesn't allow to provide the desired behaviour to them.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • 1
    hey estus! do you have any sources/links about what you explained here? I would like to take a further look! – Jota.Toledo Aug 28 '17 at 18:53
  • 1
    @Jota.Toledo Primarily my own experience with observables and third-party code that uses them. Added a reference to the manual. I would suggest to check source code first, it's the best way to figure out how Observable and Subject work internally. The parts that affect observable creation are rather simple. – Estus Flask Aug 28 '17 at 19:34
6

The two are interchangeable, but the inline documentations says that Observable.create is deprecated (future proof link here).

Mathieu CAROFF
  • 1,230
  • 13
  • 19