Created
December 17, 2023 21:41
-
-
Save altugbakan/fab95c10ad0164782177fdaaf226aaeb to your computer and use it in GitHub Desktop.
Processing Multiple Observables
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 { forkJoin, mergeAll, mergeMap, tap } from "rxjs"; | |
import { getValues, doubleValue, tripleValue } from "./service"; | |
getValues() | |
.pipe( | |
tap((value) => console.log(`Got ${value.join(", ")}\n`)), | |
mergeAll(), | |
tap((value) => console.log(`Processing ${value}`)), | |
mergeMap((value) => forkJoin([doubleValue(value), tripleValue(value)])) | |
) | |
.subscribe((value) => console.log(`Processed and got ${value.join(", ")}\n`)); | |
/* Output: | |
* | |
* Got 1, 2, 3, 4, 5 | |
* | |
* Processing 1 | |
* Processed and got 2, 3 | |
* | |
* Processing 2 | |
* Processed and got 4, 6 | |
* | |
* Processing 3 | |
* Processed and got 6, 9 | |
*/ |
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 { Observable, of } from "rxjs"; | |
export function getValues(): Observable<number[]> { | |
return of([1, 2, 3]); | |
} | |
export function doubleValue(value: number): Observable<number> { | |
return of(value * 2); | |
} | |
export function tripleValue(value: number): Observable<number> { | |
return of(value * 3); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment