What is the difference between mergeMap and mergeMapTo?

一曲冷凌霜 提交于 2019-12-10 23:34:31

问题


In rxjs5 doc, it mentions 'To reduce polymorphism and get better performance out of operators, some operators have been split into more than one operator'. What does it actually mean and how to use the mergeMapTo operator?


回答1:


From the docs, mergeMapTo:

It's like mergeMap, but maps each value always to the same inner Observable.

I see mergeMapTo as a shortcut to always output the same value. mergeMapTo doesn't care about the source value.

Also from the docs:

Maps each source value to the given Observable innerObservable regardless of the source value, and then merges those resulting Observables into one single Observable, which is the output Observable.

You'll see that mergeMap takes a function while mergeMapTo takes a value:

An example with mergeMap (we're transforming values):

Rx.Observable.of(1, 2, 3).mergeMap(x =>
  Rx.Observable.interval(1000).map(i => x+i)
);

While using mergeMapTo we can take values from a stream and always output the same value (also transforming, but always to the same value):

Rx.Observable.of(1, 2, 3).mergeMapTo(10);


来源:https://stackoverflow.com/questions/39290306/what-is-the-difference-between-mergemap-and-mergemapto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!