How to properly chain rxjs 6 observables?

前端 未结 3 1047
囚心锁ツ
囚心锁ツ 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:39

    Use a single pipe for your problem. Just comma seperate different chainable operators like map, switchMap, mergeMap, tap, etc.

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

    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). And switchMap for dispatching additional api calls.

提交回复
热议问题