大多数时候我能够避免嵌套订阅,但我不知道如何使用这段代码来做到这一点:
const appleStuff = obtainAppleStuff();
// observeAll returns Observable<Apple[]>
appleService.observeAll().subscribe(apples => {
let apple = apples.find(this.appleFilter);
if(!apple){
apple = appleService.create();
apple.type = "Red";
apple.size = 5;
appleService.update(apple);
if(apples.length !== 0){
this.appleService.observeWormsOfApple(apples[0]).subscribe(worms => {
appleService.linkWorms(worms, apple);
});
}
}
this.linkAppleStuff(appleStuff, apple);
});
理想情况下,我希望在 tap() 或单个非嵌套 subscribe() 中具有所有副作用,我该怎么做?