// RxJS v6+import { map, mergeAll } from'rxjs/operators';import { of } from'rxjs';constmyPromise= val =>newPromise(resolve =>setTimeout(() =>resolve(`Result: ${val}`),2000));//emit 1,2,3constsource=of(1,2,3);constexample=source.pipe(//map each value to promisemap(val =>myPromise(val)),//emit result from sourcemergeAll());/* output: "Result: 1" "Result: 2" "Result: 3"*/constsubscribe=example.subscribe(val =>console.log(val));
// RxJS v6+import { take, map, delay, mergeAll } from'rxjs/operators';import { interval } from'rxjs';constsource=interval(500).pipe(take(5));/* interval is emitting a value every 0.5s. This value is then being mapped to interval that is delayed for 1.0s. The mergeAll operator takes an optional argument that determines how many inner observables to subscribe to at a time. The rest of the observables are stored in a backlog waiting to be subscribe.*/constexample= source.pipe(map(val =>source.pipe(delay(1000),take(3))),mergeAll(2) ).subscribe(val =>console.log(val));/* The subscription is completed once the operator emits all values.*/