[RxJS] Use RxJS mergeMap to map and merge high order observables

Like RxJS switchMap() is a shortcut for map() and switch(), we will see in this lesson how mergeMap() is a shortcut for map() and mergeAll(), and learn its arguments for customised behavior.

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

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

const emailObservable = clickObservable
  .mergeMap(click => performRequest(), 
           (click, res) => res.email, // selector function
           3); // number of concurrency

// mergeMap = map ... + ... mergeAll

emailObservable
  .subscribe(email => console.log(email));
原文地址:https://www.cnblogs.com/Answer1215/p/6200699.html