Created
June 3, 2020 02:52
-
-
Save AnkitaSinghal/77b6e08404602831c484d99d52686451 to your computer and use it in GitHub Desktop.
Fork Join best practice
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
// Fork join - subscribe to several parallel observables, wait for all to complete and do something with the result | |
// https://medium.com/better-programming/rxjs-forkjoin-never-use-array-indexes-in-subscribe-1f4005582ae8 | |
forkJoin( | |
this.http.get('some/api/welcomeMessage'), | |
language$, | |
dateRange$ | |
// Use tab to handle data processing for each observable separately | |
.pipe(tap(([startDate, endDate]) => { | |
this.startDate = startDate; | |
this.endDate = endDate; | |
})), | |
users$.pipe(tap(users => this.setUsers(users))), | |
this.http | |
.get('some/api/chart') | |
.pipe(tap(chart => this.chartData = chart)) | |
) | |
// Use array destructuring instead of indexes for clarity | |
.subscribe(([welcomeMsg, lang]) => { | |
this.setWelcomeMessage(welcomeMsg, lang); | |
// all data aquired, wire-up everything | |
this.launch(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment