π‘ In many cases you can use mergeMap as a single operator instead!
ββββ
( example tests )
Example 1: mergeAll with promises
( StackBlitz | jsBin | jsFiddle )
// RxJS v6+import { map, mergeAll } from 'rxjs/operators';import { of } from 'rxjs';βconst myPromise = val =>new Promise(resolve => setTimeout(() => resolve(`Result: ${val}`), 2000));//emit 1,2,3const source = of(1, 2, 3);βconst example = source.pipe(//map each value to promisemap(val => myPromise(val)),//emit result from sourcemergeAll());β/*output:"Result: 1""Result: 2""Result: 3"*/const subscribe = example.subscribe(val => console.log(val));
Example 2: mergeAll with concurrent parameter
( StackBlitz | jsFiddle )
// RxJS v6+import { take, map, delay, mergeAll } from 'rxjs/operators';import { interval } from 'rxjs';βconst source = interval(500).pipe(take(5));β/*interval is emitting a value every 0.5s. This value is then being mapped to interval thatis delayed for 1.0s. The mergeAll operator takes an optional argument that determines howmany inner observables to subscribe to at a time. The rest of the observables are storedin a backlog waiting to be subscribe.*/const example = 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.*/
βmergeAll π° - Official docs
βFlatten a higher order observable with mergeAll in RxJSβ
π₯ π΅ - AndrΓ© Staltz
π Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/mergeAll.tsβ