concatMap
signature: concatMap(project: function, resultSelector: function): Observable
concatMap(project: function, resultSelector: function): ObservableMap values to inner observable, subscribe and emit in order.
Examples
Example 1: Demonstrating the difference between concatMap and mergeMap
( StackBlitz )
:bulb: Note the difference between concatMap and mergeMap. Because concatMap does not subscribe to the next observable until the previous completes, the value from the source delayed by 2000ms will be emitted first. Contrast this with mergeMap which subscribes immediately to inner observables, the observable with the lesser delay (1000ms) will emit, followed by the observable which takes 2000ms to complete.
// RxJS v6+
import { of } from 'rxjs';
import { concatMap, delay, mergeMap } from 'rxjs/operators';
//emit delay value
const source = of(2000, 1000);
// map value from source into inner observable, when complete emit result and move to next
const example = source.pipe(
concatMap(val => of(`Delayed by: ${val}ms`).pipe(delay(val)))
);
//output: With concatMap: Delayed by: 2000ms, With concatMap: Delayed by: 1000ms
const subscribe = example.subscribe(val =>
console.log(`With concatMap: ${val}`)
);
// showing the difference between concatMap and mergeMap
const mergeMapExample = source
.pipe(
// just so we can log this after the first example has run
delay(5000),
mergeMap(val => of(`Delayed by: ${val}ms`).pipe(delay(val)))
)
.subscribe(val => console.log(`With mergeMap: ${val}`));Example 2: Map to promise
( StackBlitz | jsBin | jsFiddle )
Example 3: Supplying a projection function
( StackBlitz | jsBin | jsFiddle )
Additional Resources
:newspaper: - Official docs
Use RxJS concatMap to map and concat higher order observables
:video_camera: :dollar: - André Staltz
Build your own concatMap operator
:video_camera: - Kwinten Pisman
:file_folder: Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/concatMap.ts
Last updated
Was this helpful?