您的位置:首页 > Web前端 > JavaScript

[RxJS] Use RxJS concatMap to map and concat high order observables

2016-12-19 21:49 417 查看
Like switchMap and mergeMap, concatMap is a shortcut for map() followed by a concatAll(). In this lesson we will explore this RxJS operator and its properties.

const clickObservable = Rx.Observable
.fromEvent(document, 'click');

function performRequest() {
return fetch('https://jsonplaceholder.typicode.com/users/1')
.then(res => res.json());
}

const emailObservable = clickObservable
.concatMap(click => performRequest(),
(click, res) => res.email);

// concatMap = map ... + ... concatAll
// mergeMap
// switchMap

emailObservable
.subscribe(email => console.log(email));


concatMap happens one after the other, the same as mergeAll(1):

const emailObservable = clickObservable
.mergeMap(click => performRequest(),
(click, res) => res.email,
1);


So the main different between switchMap, mergeMap, concatMap are about concurrency...

switchMap: has cancel previous request functionaility.

mergeMap: allows num of concurrency requests

concatMap: the same as mergeAll(1), only allow one request at a time
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: