Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

rxjs - What is the Difference between new Observable and of or from?

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()

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...