rxjs - 可观察的 : Complete vs finally vs done

标签 rxjs

在谈到 Observables(尤其是 rxjs)时,“finally”和“done”或“complete”有什么区别?

最佳答案

finally 总是在可观察序列终止时发生(包括错误);完成仅在它无错误终止时发生。

最后:

Invokes a specified action after the source observable sequence terminates gracefully or exceptionally.



https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/finally.md

已完成:

An Observable calls this method after it has called onNext for the final time, if it has not encountered any errors.



http://reactivex.io/documentation/observable.html

“完成”不是 rx/observables 的概念。我刚刚看到它打印在“Complete”/“OnComplete”的示例中。

注意:当您调用subscribe时,语法通常是:
observable.subscribe([observer] | [onNext], [onError], [onCompleted]);
// Like this:
observable.subscribe(
    (value) => { ... },
    (error) => { ... },
    () => { console.log('complete!'); }
);

或者
observable.subscribe({
  next: x => console.log('got value ' + x),
  error: err => console.error('something wrong occurred: ' + err),
  complete: () => console.log('done'),
});

鉴于 finally处理方式如下:
observable.finally(() => { console.log('finally!'); })
          .subscribe(...) // you can still call subscribe

关于rxjs - 可观察的 : Complete vs finally vs done,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44770999/

相关文章:

angular - 在 NGXS 中组合多个 @Select

javascript - 订阅/取消订阅 Rxjs Subject 时如何接收通知

javascript - 测试rxjs的正确方法

javascript - 使用 RxJS 在 cycle.js 中进行条件渲染

angular - RxJS Observable - 如果第一个事件在前 x 分钟内未发出,则返回,否则继续 x+y 分钟

angular - 错误 : <spyOn> : fromEvent is not declared writable or has no setter

angular - 如果条件在 RxJS 中为真,我可以跳过所有后续运算符链吗

javascript - Angular 2 RxJS 过滤器到新的 observable

angular - RxJs zip 运算符,检查哪一个抛出错误并根据条件继续?

javascript - 将 RxJS v4 代码转换为 v5,处理带有 "pull"的队列