What is "best practices" for chaining a sequence of HttpClient calls (assume the current call depends on the results of the previous calls)? The following solution is functional but apparently not recommended. Each get returns an Observable. Use of the "pipe" operator preferred in solution (the newer approach in RxJs).
ngOnInit() {
this.firstService.get().subscribe((first) => {
this.secondService.get().subscribe(second => {
this.thirdService.get().subscribe(third => {
... possibly more nested calls ...
})
})
})
}
Your code is far beyond best practice. Never do subscription inside another.
If your tasks are three seperate tasks/observables which do not depends each other, then consider to use
forkJoin
(all Observables start at the same time and when the last observable finishes It returns result)If their results depends on each other you can use
switchMap
,flatMap
,mergeMap
,exhaustMap
(check differences)