Last active
January 12, 2024 16:29
-
-
Save jhades/e1a6c3f4449628c5cd794d9f72abda37 to your computer and use it in GitHub Desktop.
RxJs SwitchMap Operator
This file contains 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 {delay} from "rxjs/operators"; | |
function simulateHttp(val: any, delay:number) { | |
return of(val).pipe(delay(delay)); | |
} |
This file contains 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
console.log('simulating HTTP requests'); | |
const http1$ = simulateHttp("1", 1000); | |
const http2$ = simulateHttp("2", 1000); |
This file contains 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
http1$.subscribe( | |
console.log, | |
console.error, | |
() => console.log('http1$ completed') | |
); | |
http2$.subscribe( | |
console.log, | |
console.error, | |
() => console.log('http2$ completed') | |
); |
This file contains 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
http1$.subscribe( | |
val => console.log(val), | |
err => console.error(err), | |
() => console.log('http1$ completed') | |
); |
This file contains 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
const saveUser$ = simulateHttp(" user saved ", 1000); | |
const httpResult$ = saveUser$.pipe( | |
switchMap(sourceValue => { | |
console.log(sourceValue); | |
return simulateHttp(" data reloaded ", 2000); | |
} | |
) | |
); | |
httpResult$.subscribe( | |
console.log, | |
console.error, | |
() => console.log('completed httpResult$') | |
); |
This file contains 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 {interval} from 'rxjs'; | |
import {map} from "rxjs/operators"; | |
function simulateFirebase(val: any, delay: number) { | |
return interval(delay).pipe(map(index => val + " " + index)); | |
} |
This file contains 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
const firebase1$ = simulateFirebase("FB-1 ", 5000); | |
const firebase2$ = simulateFirebase("FB-2 ", 1000); | |
firebase1$.subscribe( | |
console.log, | |
console.error, | |
() => console.log('firebase1$ completed') | |
); | |
firebase2$.subscribe( | |
console.log, | |
console.error, | |
() => console.log('firebase2$ completed') | |
); |
This file contains 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
const firebase1$ = simulateFirebase("FB-1 ", 5000); | |
const firebase2$ = simulateFirebase("FB-2 ", 1000); | |
const firebaseResult$ = firebase1$.pipe(switchMap(sourceValue => { | |
console.log("source value " + sourceValue); | |
return simulateFirebase("inner observable ", 1000) | |
})); | |
firebaseResult$.subscribe( | |
console.log, | |
console.error, | |
() => console.log('completed firebaseResult$') | |
); | |
This file contains 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
const course$ = simulateHttp({id:1, description: 'Angular For Beginners'}, 1000); | |
const httpResult$ = course$.pipe(switchMap( | |
sourceValue => simulateHttp([... returns a lessons array ...], 2000))); | |
httpResult$.subscribe( | |
console.log, | |
console.error, | |
() => console.log('completed httpResult$') | |
); | |
This file contains 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
const course$ = simulateHttp({id:1, description: 'Angular For Beginners'}, 1000); | |
const httpResult$ = course$.pipe( | |
switchMap(courses => simulateHttp([], 2000) | |
.pipe( | |
map(lessons => [courses,lessons]) | |
), | |
) | |
); | |
httpResult$.subscribe( | |
console.log, | |
console.error, | |
() => console.log('completed httpResult$') | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is the code of 08.ts correct?
Shouldn't
look like