Any suggestions, how this can be rewritten in more promise-chaining style?:
this.apiService.sendPutRequest('/api/users/activate', usrObj).pipe(
map(() => {
return this.apiService.sendGetRequest('/api/users/' + this.currentUserId).pipe(
map(data => {
return this.setActiveUser(data).pipe(
map(() => {
return this.apiService.sendGetRequest('api/tasks/user/' + this.currentUserId).pipe(
map(tasks => {
return this.taskService.setCurrentUserTasks(tasks);
})
);
})
);
})
);
})
);
use
SwitchMap
for that.Use a single
pipe
for your problem. Just comma seperate different chainable operators likemap
,switchMap
,mergeMap
,tap
, etc.Simplified: Use
map
if you just want to transform a value without any async api calls and pass it to another operator or a subscription,tap
if you just want to catch values in between without transforming (e.g. for logging). AndswitchMap
for dispatching additional api calls.You can use switchMap for handling observables and tap for side efects handling. And you need to subscribe because it's cold observable
For error handling use catchError for all requests