Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...