Skip to content

Commit 2bb204d

Browse files
committed
promise
1 parent 7eb7362 commit 2bb204d

File tree

4 files changed

+1696
-0
lines changed

4 files changed

+1696
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

es2015/promise.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//调用resolve或reject并不会终结 Promise 的参数函数的执行。所以,最好在它们前面加上return语句,这样就不会有意外。
2+
3+
//如果 p2 返回p1,这时p1的状态就会传递给p2,也就是说,p1的状态决定了p2的状态。如果p1的状态是pending,
4+
//那么p2的回调函数就会等待p1的状态改变;如果p1的状态已经是resolved或者rejected,那么p2的回调函数将会立刻执行。
5+
6+
//如果 Promise 状态已经变成resolved,再抛出错误是无效的。
7+
const promise = new Promise(function(resolve, reject) {
8+
resolve('ok');
9+
throw new Error('test');
10+
});
11+
promise
12+
.then(function(value) { console.log(value) })
13+
.catch(function(error) { console.log(error) });
14+
// ok
15+
16+
const promise = new Promise(function (resolve, reject) {
17+
resolve('ok');
18+
setTimeout(function () { throw new Error('test') }, 0)
19+
});
20+
promise.then(function (value) { console.log(value) });
21+
// ok
22+
// Uncaught Error: test
23+
//Promise 指定在下一轮“事件循环”再抛出错误。到了那个时候,Promise 的运行已经结束了,所以这个错误是在 Promise 函数体外抛出的,会冒泡到最外层,成了未捕获的错误。
24+
25+
//promise.all
26+
//如果作为参数的 Promise 实例,自己定义了catch方法,那么它一旦被rejected,并不会触发Promise.all()的catch方法
27+
28+
setTimeout(function () {
29+
console.log('three');
30+
}, 0);
31+
32+
Promise.resolve().then(function () {
33+
console.log('two');
34+
});
35+
36+
console.log('one');
37+
38+
// one
39+
// two
40+
// three 立即resolve的 Promise 对象,是在本轮“事件循环”(event loop)的结束时,而不是在下一轮“事件循环”的开始时。

es2015/reflect.js

Whitespace-only changes.

0 commit comments

Comments
 (0)