3 回答
TA贡献1780条经验 获得超3个赞
像这样的东西应该使用 switchMap 运算符
this.projectService.addProject(newProject).pipe(
tap(data => newProjectFromServer = data),
switchMap(() => {
return this.projectService.addEmployeesToProject(
newProjectFromServer.id,
newProject.assignedEmployees
)}
).subscribe((secondResult) => {
this.afterSubmit()
});
我希望它有所帮助。
有关 switchMap 运算符的研究链接:
https://www.learnrxjs.io/learn-rxjs/operators/transformation/switchmap
TA贡献1911条经验 获得超7个赞
您可以使用concatMap、switchMap、mergeMap。建议使用concatMap,因为它可以确保 HTTP 调用的顺序执行。
this.projectService.addProject(newProject).pipe(
concatMap((newProjectFromServer) => {
this.projectService.addEmployeesToProject(
newProjectFromServer.id,
newProject.assignedEmployees
)}
).subscribe((secondResult) => {
this.afterSubmit()
});
TA贡献1801条经验 获得超8个赞
我认为更好的方法是将其转换为承诺:
public async myFunc(): Promise<void> {
const newProjectFromServer = await this.projectService.addProject(newProject).toPromise()
await this.projectService
.addEmployeesToProject(
newProjectFromServer.id,
newProject.assignedEmployees
).toPromise();
this.afterSubmit();
}
添加回答
举报