【发布时间】:2019-06-03 00:58:04
【问题描述】:
我在subscribe 内有带有subscribe 的代码:
this.http.post('/update1', newData).subscribe(
(response) => {
const r = response.json;
const id = r['id'];
this.http.post('/update2?id=' + id, newData).subscribe(
() => {
this.http.post('/update3?id=' + id, newData).subscribe(
() => {
console.log('success');
},
() => {
// revert changes made to update2
this.http.post('/update2', previousValues).subscribe();
// revert changes made to update1
this.http.post('/update1', previousValues).subscribe();
});
},
() => {
// revert changes made to update1
this.http.post('/update1', previousValues).subscribe();
}
);
},
(error) => {
console.log(error);
}
);
如何在 RxJS 5 中优化它?
【问题讨论】:
-
使用
mergeMap或concatMap而不是第一个subscribe -
goga 所说的......你永远不需要嵌套订阅。这可能会对您有所帮助:blog.strongbrew.io/rxjs-best-practices-in-angular
标签: angular typescript rxjs rxjs5