promise和Observables的区别

简介: StackOverflow上有一个讨论:What is the difference between Promises and Observables?一篇有用的博客:Angular2 Observables, Http, and separating services and componentsAngular官网的一篇文章:Observables compared to other techniquesObservable和promise的区别:

订阅专栏

StackOverflow上有一个讨论: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.


相关文章
|
1月前
|
前端开发
Await和Async是什么?跟Promise有什么区别 使用它有什么好处
Await和Async是什么?跟Promise有什么区别 使用它有什么好处
|
4天前
|
前端开发
Promise和async/await之间有什么区别
Promise与async/await是异步编程的两种模式。Promise使用.then()和.catch()处理回调,语法较复杂,易出现回调地狱;而async/await提供更清晰的顺序代码,使用try/catch进行错误处理,使异步操作更易读、易维护。Promise在控制流和错误堆栈方面较为灵活,但定位错误难,而async/await自动等待、线性控制流,错误堆栈清晰。两者各有优势,选择取决于具体需求和偏好。
|
1月前
|
前端开发
promise和async的区别是什么?
promise和async的区别是什么?
11 1
|
6月前
|
前端开发
promise中reject和catch处理上有什么区别
promise中reject和catch处理上有什么区别
71 0
|
10月前
|
存储 前端开发 JavaScript
|
9月前
Promise.all和Promise.race的区别和使用
比如当数组里的P1,P2都执行完成时,页面才显示。 值得注意的是,返回的数组结果顺序不会改变,即使P2的返回要比P1的返回快,顺序依然是P1,P2 Promise.all成功返回成功数组, 失败返回失败数据,一但失败就不会继续往下走
|
9月前
|
前端开发
Promise与async/await的区别?
Promise与async/await的区别?
|
12月前
|
前端开发 C++
【面试】‘return await promise‘ 与 ‘return promise‘ 这细微的区别,你的可能还不知道?
当从一个异步函数的promise返回时,我们可以使用return await promise等待 promise 解析完,也可以直接返回它 return promise。
|
前端开发 JavaScript
setTimeout、Promise、 Async/Await 的区别
setTimeout、Promise、 Async/Await 的区别
148 0
setTimeout、Promise、 Async/Await 的区别
|
前端开发
简单说下Promise.all和Promise.race区别和使用场景
简单说下Promise.all和Promise.race区别和使用场景
100 0
简单说下Promise.all和Promise.race区别和使用场景