What is the difference between Promises and Observables?

后端 未结 30 3177
小鲜肉
小鲜肉 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:33

    Something I ran into that wasn't apparent from a first reading of the tutorial and docs was the idea of multicasting.

    Make sure you're aware that by default, multiple subscriptions will trigger multiple executions in an Observable. Multiple subscriptions to a single HTTP call Observable will trigger multiple identical HTTP calls unless you .share() (enable multicasting).

    A promise forces you to deal with one thing at a time, unwrap its data, handle exceptions, has language support for cool things like async/await, and is pretty barebones otherwise.

    An Observable has lots of bells and whistles, but you need to understand the power you're working with or it can be misused.

提交回复
热议问题