What is the difference between Promises and Observables?

后端 未结 30 2603
小鲜肉
小鲜肉 2020-11-21 23:48

What is the difference between Promise and Observable in Angular?

An example on each would be helpful in understanding both the cases. In w

30条回答
  •  隐瞒了意图╮
    2020-11-22 00:19

    Observables are often compared to promises. Here are some key differences:

    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.

    Observables provide many values. Promises provide one. This makes observables useful for getting multiple values over time.

    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 parts of the system, without causing the work to be executed.

    Observables subscribe() is responsible for handling errors. Promises push errors to the child promises. This makes observables useful for centralized and predictable error handling.

    That is the most simplest difference that you may found on ANGULAR.IO docs. rest answer is given by most is correct at its own place

提交回复
热议问题