1

Major difference between of and from is that of returns all the values at once and from returns them one by one.

Does this mean that if we use of with Observables, they start behaving in a synchronous way?

And the way to make them behave asynchronous is to use from?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

3 Answers3

2

Everything in RxJS is synchronous unless you work with delays or you intentionally use an asynchronous scheduler such as asyncScheduler.

When you have an RxJS chain, whether it's synchronous or asynchronous only depends on the operators you use and on the behavior of your source Observables. So this isn't anything specific to of() or from(). Even when from([1, 2, 3]) or of(1, 2, 3) emits three values it'll be emitted synchronously.

Also be aware that there's no way you can turn an asynchronous chain into behaving synchronously.

martin
  • 93,354
  • 25
  • 191
  • 226
  • So basically one more difference between observables and promises is that observables are synchronous and promises are not? – Aquarius_Girl Oct 05 '20 at 08:46
  • Not quite. `Promise` is always asynchronous, `Observable` can be either. It depends on the operators used to build/manipulate it. Here is a write up on the differences: https://medium.com/javascript-everyday/javascript-theory-promise-vs-observable-d3087bc1239a – Gunnar B. Oct 05 '20 at 09:26
  • @GunnarB. That's why I'm specifically mentioning *behavior of your source Observables*. Promise is always wrapped with an Observable but that has no effect on how in this case arrays are processed by `from()`. – martin Oct 05 '20 at 11:08
  • @Aquarius_Girl Observables can be synchronous like in this case `from([1, 2, 3])` or asynchronous like `timer(0, 1000)`. Maybe if you could make a demo of what behavior is not clear to you that would help. – martin Oct 05 '20 at 11:13
  • @martin That was meant as a reply to Aquarius_Girls question, not your answer ;) – Gunnar B. Oct 05 '20 at 17:37
1

There is a slight misconception here (as implied by Kaustubh):
of does not return all its values at once. It is just that of can take a variable amount of arguments whereas from takes a single.

of interprets every single given argument as value while from will turn the single given argument into an observable.

So this behaves the same:

of(1, 2)
from([1, 2])

while this does not:

of([1, 2])
from([1, 2])
Gunnar B.
  • 2,879
  • 2
  • 11
  • 17
  • That is imprecise. if you run `from(Promise.resolve('hello')).subscribe(console.log)` I will receive `hello` while if I run `of(Promise.resolve('hello')`, I will receive `[object Promise]` – Guilhermevrs Oct 05 '20 at 06:18
  • 1
    @Guilhermevrs In which sense is that imprecise? It actually does exactly what I wrote: `from` turns the promise into an observable and `of` interprets the promise as value which will be a promise object. – Gunnar B. Oct 05 '20 at 07:56
0

Consider the following,

const myValues = ['val1', 'val2', 'val3'];

const atOnce = Observable.of(myValues); // This will emit the entire myValues array as a single emission.

// The Observables below are similar in behaviour
// The will emit the values from myValues one at a time

const discreteOne = Observable.of(...myValues);
const discreteTwo = Observable.from(myValues);

Refer this post for more.

Kaustubh Badrike
  • 580
  • 2
  • 15