1

What is the Difference between new Observable and observable created from of or from?

of([1, 2, 3]).subscribe(x => console.log(x));

from([1, 2, 3]).subscribe(x => console.log(x));

vs new Observable()

What is the main difference between the above two way of creating observables?

I have read this, but it's not yet convincing! what is the difference between "new Observable()" and "of()" in RxJs

I'm not asking difference between of and from. I'm asking difference between either (of or from) from new Observable()

mx_code
  • 2,441
  • 1
  • 12
  • 37
  • `of` and `from` return two very specific observables. It's all explained in https://rxjs-dev.firebaseapp.com/guide/observable. I downvote because this question is answered in the first paragraph of the first page of the official guide. – Thomas Sablik Mar 19 '21 at 00:21
  • What do you mean with the duplicate is very convincing? Please elaborate the difference between your question and the duplicate. – Thomas Sablik Mar 19 '21 at 00:32
  • I'm not asking difference between of and from. I'm asking difference between either (of or from) from new Observable() – mx_code Mar 19 '21 at 00:33
  • Please read carefully. My question is concise and clear. There is no need for elaboration as there is nothing to elaborate. – mx_code Mar 19 '21 at 00:34
  • Yes, I understand. The answer is that `of` and `from` return two very specific observables while `new Observable` creates a custom observable. It's an exact duplicate of the linked question and described on the first page of the official guide. If you don't want to read the duplicate or the guide why would you read a third version as an answer? – Thomas Sablik Mar 19 '21 at 00:35
  • What does that mean a custom observable. A – mx_code Mar 19 '21 at 00:35
  • Aren't we creating custom observables with of and from also? – mx_code Mar 19 '21 at 00:36
  • No, they have very specific behavior. You can't change the behavior. You can only change the values. – Thomas Sablik Mar 19 '21 at 00:37
  • Can you elaboarte with an examble. – mx_code Mar 19 '21 at 00:38
  • There are multiple examples in the guide and in the duplicate question. Would it really help if I copy them for you? You just have to click the links and read 5 minutes. The first example is literally in the first paragraph. – Thomas Sablik Mar 19 '21 at 00:39
  • _"Example. The following is an Observable that pushes the values 1, 2, 3 immediately (synchronously) when subscribed, and the value 4 after one second has passed since the subscribe call, then completes"_ That's not possible with `of` and `from`. That's the main difference. – Thomas Sablik Mar 19 '21 at 00:44

2 Answers2

3

Essentially, the observable creation functions of, from, and others simply create a new Observable() with specific behavior.

So, the only difference between new Observable() and the creator fuctions is the actual behavior.

For example, here is what the of() function looks like (simplified):

export function of<T>(...array: Array<T>): Observable<T> {
  return new Observable<T>(subscriber => {
    for (let i = 0; i < array.length && !subscriber.closed; i++) {
      subscriber.next(array[i]);
    }
    subscriber.complete();
  });
}

You can see that new Observable() is called within the of() function.

Dharman
  • 30,962
  • 25
  • 85
  • 135
BizzyBob
  • 12,309
  • 4
  • 27
  • 51
-1

The difference between new Observable and oforfromis thatofandfromare functions that utilizenew Observableto create new observable with a fixed observer whilenew Observablecan be called with any possible observer. Withnew Observableyou can create any observable, even the observables created withofandfrom`.

It's like asking about the difference between a mathematical function and a polynomial.