使用 RxJS observables 在 observable returns 之后采取行动时,'do' 和 'finally' 有什么区别?

What is the differnce between 'do' and 'finally' when using RxJS observables to take action after the observable returns?

我没有看到很好的文档来说明 RxJS 中 dofinally 之间的区别。我的目标是仅当 Observable returns 数据时才采取行动,但看起来它们也会在失败时采取行动。

observable 可能 return 不止一个元素?

我希望有人能解释一下 使用哪种方法或是否有更好的替代方法很重要。

    getData(choice): void {
        this.dataService.getTableData(choice, 'mainCalls.php')
            .do( () => this.defineWidth() )
            .subscribe(tableData => this.tableData = tableData,
                err => {
                    console.log(err);
                }
            );
    }

    ngOnInit() {
        this.getData('getTableData');
    }

defineWidth 是一个函数,它依赖于被 Observable return 编辑的数据。我乐于接受建议并阅读 material 替代方法来完成我想要的。

do() 为每个正常事件调用并且不修改数据流。它仅用于副作用。

finally() 在最后一个事件之后或在出现错误(如果有)之后调用一次。无论是成功还是失败,它都会被调用一次。

如果 this.defineWidth() 依赖于 this.tableData,那么您不需要 dofinally。只需在将响应分配给 this.tableData:

的行之后添加调用
getData(choice): void {
    this.dataService.getTableData(choice, 'mainCalls.php')
        .subscribe(tableData => {
           this.tableData = tableData;
           this.defineWidth();
         }),
         err => {
           console.log(err);
         }
    );
}