How to properly chain rxjs 6 observables?

前端 未结 3 1065
囚心锁ツ
囚心锁ツ 2021-01-12 17:40

Any suggestions, how this can be rewritten in more promise-chaining style?:

this.apiService.sendPutRequest(\'/api/users/activate\', usrObj).pipe(
        map         


        
3条回答
  •  既然无缘
    2021-01-12 18:27

    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

    this.apiService.sendPutRequest('/api/users/activate', usrObj).pipe(
        catchError(err=> this.errorHandler(err)),
        switchMap(() => this.apiService.sendGetRequest('/api/users/' + this.currentUserId)
            .pipe(catchError(err=> this.errorHandler(err)))
        ),
        tap(data => this.setActiveUser(data)),
        switchMap(() => this.apiService.sendGetRequest('api/tasks/user/' + this.currentUserId)
            .pipe(catchError(err=> this.errorHandler(err)))
        ),
        tap(tasks => this.taskService.setCurrentUserTasks(tasks))
    ).subscribe()
    

提交回复
热议问题