0 day streak
javascript / intermediate
Snippet

Synchronizing Parallel Requests with forkJoin

When you need to fetch multiple independent resources before initializing a component, forkJoin allows you to run them in parallel and emits a single object containing all results once every request has completed.

snippet.js
javascript
1
2
3
4
forkJoin({
profile: this.http.get('/api/profile'),
settings: this.http.get('/api/settings')
}).subscribe(data => this.initialize(data));
angular
Breakdown
1
forkJoin({ profile: ..., settings: ... })
Combines multiple observables into a dictionary-like structure for parallel execution.
2
subscribe(data => this.initialize(data))
Triggers only after all observables have emitted their final value and completed.