如何正确链接 rxjs 6 observables?
How to properly chain rxjs 6 observables?
任何建议,如何以更多的 promise 链式重写?:
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);
})
);
})
);
})
);
})
);
为此使用 SwitchMap
。
mainApiCall.pipe(
switchMap(result=>secondApiCall(result)),
switchMap(resultFromSecondApiCall=>thirdApiCall(resultFromSecond))
...
and so on
)
您可以使用switchMap for handling observables and tap for side efects handling. And you need to subscribe because it's cold observable
对于错误处理,对所有请求使用 catchError
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()
使用一个 pipe
来解决您的问题。只需用逗号分隔不同的可链接运算符,例如 map
、switchMap
、mergeMap
、tap
等
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))
);
简化:如果您只是想在没有任何异步 api 调用的情况下转换值并将其传递给另一个运算符或订阅,请使用 map
,如果您只是想在不转换的情况下捕获两者之间的值(例如用于日志记录)。 switchMap
用于调度额外的 api 个调用。
任何建议,如何以更多的 promise 链式重写?:
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);
})
);
})
);
})
);
})
);
为此使用 SwitchMap
。
mainApiCall.pipe(
switchMap(result=>secondApiCall(result)),
switchMap(resultFromSecondApiCall=>thirdApiCall(resultFromSecond))
...
and so on
)
您可以使用switchMap for handling observables and tap for side efects handling. And you need to subscribe because it's cold observable
对于错误处理,对所有请求使用 catchError
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()
使用一个 pipe
来解决您的问题。只需用逗号分隔不同的可链接运算符,例如 map
、switchMap
、mergeMap
、tap
等
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))
);
简化:如果您只是想在没有任何异步 api 调用的情况下转换值并将其传递给另一个运算符或订阅,请使用 map
,如果您只是想在不转换的情况下捕获两者之间的值(例如用于日志记录)。 switchMap
用于调度额外的 api 个调用。