Created
November 20, 2018 12:40
-
-
Save jokernix/a13ba0d84765c92fe51747b2a0a8ba65 to your computer and use it in GitHub Desktop.
Demonstrating the difference between concatMap and mergeMap and switchMap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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