Skip to content

Instantly share code, notes, and snippets.

@AnkitaSinghal
Created June 3, 2020 02:52
Show Gist options
  • Save AnkitaSinghal/77b6e08404602831c484d99d52686451 to your computer and use it in GitHub Desktop.
Save AnkitaSinghal/77b6e08404602831c484d99d52686451 to your computer and use it in GitHub Desktop.
Fork Join best practice
// 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