File tree Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Original file line number Diff line number Diff line change 4545> 基础
4646
47471 . [ “use strict” 是如何工作的?] ( ./contents/basic/use_strict.md )
48- 2 . [ 闭包是如何工作的 ?] ( ./contents/basic/closures.md )
48+ 2 . [ 闭包 是如何工作的 ?] ( ./contents/basic/closures.md )
49493 . [ new 是如何工作的?] ( ./contents/basic/new.md )
50504 . [ this 的运行原理?] ( ./contents/basic/this.md )
51515 . [ 在 JavaScript 中循环遍历数组] ( ./contents/basic/loopArray.md )
616115 . [ 在 JavaScript 中如何把字符转换成ASCII编码?] ( ./contents/basic/ascii.md )
626216 . [ 在 JavaScript 中 addEventListener 和 onclick 之间有什么区别?] ( ./contents/basic/onclick.md )
636317 . [ 在 JavaScript 中如何判断一个 number类型 是浮点数还是整数?] ( ./contents/basic/integerVsFloat.md )
64+
6465- todo ...
6566
6667> 进阶
70713 . [ 什么时候应该使用encodeURI,而不是encodeURIComponent?] ( ./contents/advanced/encode.md )
71724 . [ WebSocket 和 Socket.IO 之间的区别] ( ./contents/advanced/socketVSwebscocket.md )
72735 . [ ES6 中 WeakMap 的实际用途是什么?] ( ./contents/advanced/weakMap.md )
74+ 6 . [ Promise 和 Observables 之间的区别] ( ./contents/advanced/observable.md )
75+
7376- todo ...
7477
7578> Typescript
7679
77801 . [ TypeScript是什么,为什么要用它来代替JavaScript?] ( ./contents/typescript/index.md )
78812 . [ TypeScript中 接口(interface) 与 类型(type) 的区别是什么?] ( ./contents/typescript/interface.md )
82+
7983- todo ...
8084
8185> React
8286
83871 . [ React 中, state 和 props 的区别是什么?应该怎么使用?] ( ./contents/react/stateVsProps.md )
84882 . [ React Context 和 React Redux 的区别是什么?应该怎么使用?] ( ./contents/react/context.md )
89+
8590- todo ...
8691
8792> Node.js
8893
89941 . [ package.json 中,版本前的符号 ~ 和 ^ 有什么区别?] ( ./contents/node/packageJSON.md )
95+
9096- todo ...
Original file line number Diff line number Diff line change 1+ ## 问题:Promise 和 Observables 之间的区别
2+
3+ 在 ` Angular/RxJs ` 中, ` Promise ` 和 ` Observable ` 的区别是什么?
4+
5+ ## 答案
6+
7+ ### Promise
8+
9+ ` Promise ` 擅长处理单一的事件,比如等待一个异步操作,监听它的完成或失败。
10+
11+ 注意:目前有一些支持取消的 ` Promise ` 库,但到目前为止还没有官方的取消规范。
12+
13+ ### Observable
14+
15+ ` Observable ` 就像 ` Stream ` ,允许传递零或多个事件,每个事件的回调都会进行调用。
16+
17+ 它也比 ` Promise ` 更灵活一些,因为它提供了比 ` Promise ` 更多的功能。
18+
19+ 有了 ` Observable ` ,无论你想多少个事件都不重要,你可以使用相同的 ` API ` 处理不同的情况。
20+
21+ 与 ` Promise ` 相比,` Observable ` 还具有可取消的优势。
22+
23+ ` Observable ` 的订阅是允许取消的,当你不再需要服务器的 ` HTTP ` 请求或其他昂贵的异步操作的响应结果时,随时可以取消。
24+
25+ 而 ` Promise ` 一旦执行,始终会调用成功或失败,即使你不再需要它提供的结果。
26+
27+ ` Promise ` 会立即启动,而 ` Observable ` 只有在你订阅它时才会启动,因为 ` Observable ` 是惰性的。
28+
29+ ` Observable ` 提供了类似数组的一些操作符:像 map、forEach、reduce..., 方便你对异步的结果进行处理。当然,也有一些更强大的 [ RxJS操作符] ( https://rxjs-dev.firebaseapp.com/guide/operators )
30+
31+ 因其惰性执行的特性,可以在观察器被订阅执行之前建立一连串的操作符,以进行更加声明式的编程方式。
32+
33+ > 问题来源:[ https://stackoverflow.com/questions/37364973/what-is-the-difference-between-promises-and-observables ] ( https://stackoverflow.com/questions/37364973/what-is-the-difference-between-promises-and-observables )
You can’t perform that action at this time.
0 commit comments