Skip to content

Instantly share code, notes, and snippets.

@jokernix
Created November 20, 2018 12:40
Show Gist options
  • Save jokernix/a13ba0d84765c92fe51747b2a0a8ba65 to your computer and use it in GitHub Desktop.
Save jokernix/a13ba0d84765c92fe51747b2a0a8ba65 to your computer and use it in GitHub Desktop.
Demonstrating the difference between concatMap and mergeMap and switchMap
import { of } from 'rxjs';
import { concatMap, delay, mergeMap, switchMap } from 'rxjs/operators';
const source = of(2000, 1000);
source
.pipe(concatMap(val => of(`Delayed by: ${val}ms`).pipe(delay(val))))
.subscribe(val => console.log(`With concatMap: ${val}`));
source
.pipe(
delay(5000),
mergeMap(val => of(`Delayed by: ${val}ms`).pipe(delay(val)))
)
.subscribe(val => console.log(`With mergeMap: ${val}`));
source
.pipe(
delay(10000),
switchMap(val => of(`Delayed by: ${val}ms`).pipe(delay(val)))
)
.subscribe(val => console.log(`With switchMap: ${val}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment