What are the differences between observables and promises in JavaScript?

后端 未结 7 800
隐瞒了意图╮
隐瞒了意图╮ 2020-12-23 12:48

So i\'ve read that observables are looking to overtake promises in terms of usage in some of upcoming JavaScript MVC\'s:

  • Angular 2.0
  • Falcor used by Ne
7条回答
  •  既然无缘
    2020-12-23 13:46

    Promises are a representation of 1 future value. Observables are a representation for a possibly infinite amount of values.

    Promises will trigger the fetching of that value immediately upon creation. Observables will only start producing values when you subscribe to them. (Unless it's a hot observable, but that's outside the scope of this question)

    Promises are designed to represent AJAX calls. Observables are designed to represent anything: events, data from databases, data from ajax calls, (possibly infinite) sequences, etc.

提交回复
热议问题