欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

rxjs 'of' vs 'from' operator 区别 有大用

Asked 
Active 10 days ago
Viewed 34k times
121

Is the only difference between Observable.of and Observable.from the arguments format? Like the Function.prototype.call and Function.prototype.apply?

Observable.of(1,2,3).subscribe(() => {})
Observable.from([1,2,3]).subscribe(() => {})

3 Answers 正确答案

85

Not quite. When passing an array to Observable.from, the only difference between it and Observable.of is the way the arguments are passed.

However, Observable.from will accept an argument that is

a subscribable object, a Promise, an Observable-like, an Array, an iterable or an array-like object to be converted

There is no similar behaviour for Observable.of - which always accepts only values and performs no conversion.

145

It is important to note the difference between of and from when passing an array-like structure (including strings):

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

would print the whole array at once.

On the other hand,

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

prints the elements 1 by 1.

For strings the behaviour is the same, but at character level.

10

Another interesting fact is Observable.of([]) will be an empty array when you subscribe to it. Where as when you subscribe to Observable.from([]) you wont get any value.

This is important when you do a consecutive saving with switchmap. Ex:

.do((data) => {
            this.jobService.save$.next(this.job.id);
        })
        .switchMap(() => this.jobService.addSites(this.job.id, this.sites)
            .flatMap((data) => {
                if (data.length > 0) {
                    // get observables for saving
                    return Observable.forkJoin(jobSiteObservables);
                } else {
                    **return Observable.of([]);**
                }
            })).do((result) => {
            // ..
        })
        .switchMap(() => this.saveComments())
....

if data.length = 0 in addSite section, the above code is returning Observable.of([]) and then goes to save comments. But if you replace it with Observable.from([]), the succeeding methods will not get called.

rxfiddle

来自  https://stackoverflow.com/questions/42704552/of-vs-from-operator


 
活动 10天之前
观看了 34k次
121

Observable.ofObservable.from参数格式之间唯一的区别是喜欢Function.prototype.callFunction.prototype.apply

Observable.of(1,2,3).subscribe(() => {})
Observable.from([1,2,3]).subscribe(() => {})

3个答案 

正确答案



85

不完全的。将数组传递给时Observable.from,它和之间唯一的区别Observable.of就是传递参数的方式。

然而,Observable.from将接受的说法

一个可订阅的对象,一个Promise,一个类似Observable的对象,一个数组,一个可迭代或类似数组的要转换的对象

Observable.of没有类似的行为-它始终仅接受值并且不执行任何转换。

145

重要的是要注意offrom传递类似数组的结构(包括字符串)之间的区别

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

将立即打印整个数组。

另一方面,

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

按1打印元素。

对于字符串,行为是相同的,但是在字符级别。

10

另一个有趣的事实是Observable.of([])订阅时将为空数组。当您订阅Observable.from([])时,您将不会获得任何价值。

当使用switchmap进行连续保存时,这一点很重要。例如:

.do((data) => {
            this.jobService.save$.next(this.job.id);
        })
        .switchMap(() => this.jobService.addSites(this.job.id, this.sites)
            .flatMap((data) => {
                if (data.length > 0) {
                    // get observables for saving
                    return Observable.forkJoin(jobSiteObservables);
                } else {
                    **return Observable.of([]);**
                }
            })).do((result) => {
            // ..
        })
        .switchMap(() => this.saveComments())
....

如果addSite部分中的data.length = 0,则上面的代码返回Observable.of([]),然后保存注释。但是,如果将其替换为Observable.from([]),将不会调用后续方法。

rxfiddle

来自  https://stackoverflow.com/questions/42704552/of-vs-from-operator



普通分类: