订阅专栏
*上有一个讨论:What is the difference between Promises and Observables?
一篇有用的博客:Angular2 Observables, Http, and separating services and components
Angular官网的一篇文章:Observables compared to other techniques
Observable和promise的区别:
Observables are declarative; computation does not start until subscription. Promises execute immediately on creation. This makes observables useful for defining recipes that can be run whenever you need the result.
Observable是陈述式的,计算直至subscription才会触发。这和promise很不同——promise创建之后,立即执行。
Observables provide many values. Promises provide one. This makes observables useful for getting multiple values over time.
Observable提供多个值,而promises只能提供单个值。
Observables differentiate between chaining and subscription. Promises only have .then() clauses. This makes observables useful for creating complex transformation recipes to be used by other part of the system, without causing the work to be executed.
Observable可以借助rxjs丰富的Operators,组合成复杂的transformation recipes, 便于应用内的重用,而promise只有一个then操作。
Observables subscribe() is responsible for handling errors. Promises push errors to the child promises. This makes observables useful for centralized and predictable error handling.