Skip to content

Commit 032e950

Browse files
committed
修改promise
1 parent 4bba9a0 commit 032e950

File tree

3 files changed

+82
-21
lines changed

3 files changed

+82
-21
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
全书已由电子工业出版社出版([版权页](images/copyright.png)[内页1](images/page1.png)[内页2](images/page2.png)),铜版纸全彩印刷,附有索引。欢迎大家购买,支持开源项目。
1212

13-
- [亚马逊](http://www.amazon.cn/%E5%9B%BE%E4%B9%A6/dp/B00MQKRLD6/)
1413
- [京东](http://item.jd.com/11526272.html)
1514
- [当当](http://product.dangdang.com/23546442.html)
15+
- [亚马逊](http://www.amazon.cn/%E5%9B%BE%E4%B9%A6/dp/B00MQKRLD6/)
1616
- [China-pub](http://product.china-pub.com/4284817)
1717

1818
### 版权许可

docs/promise.md

+79-19
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,45 @@ var getJSON = function(url) {
6060
client.send();
6161

6262
function handler() {
63-
if (this.readyState === this.DONE) {
64-
if (this.status === 200) {
65-
resolve(this.response);
66-
} else {
67-
reject(this);
68-
}
69-
}
63+
if (this.status === 200) {
64+
resolve(this.response);
65+
} else {
66+
reject(new Error(this.statusText));
67+
}
7068
};
7169
});
7270

7371
return promise;
7472
};
7573

7674
getJSON("/posts.json").then(function(json) {
77-
// continue
75+
console.log('Contents: ' + value);
7876
}, function(error) {
79-
// handle errors
77+
console.error('出错了', reason);
8078
});
8179

8280
```
8381

84-
## 链式操作
82+
上面代码中,resolve方法和reject方法调用时,都带有参数。它们的参数会被传递给回调函数。reject方法的参数通常是Error对象的实例,而resolve方法的参数除了正常的值以外,还可能是另一个Promise实例,比如像下面这样。
8583

86-
then方法返回的是一个新的Promise对象,因此可以采用链式写法。
84+
```javascript
85+
86+
var p1 = new Promise(function(resolve, reject){
87+
// ... some code
88+
});
89+
90+
var p2 = new Promise(function(resolve, reject){
91+
// ... some code
92+
resolve(p1);
93+
})
94+
95+
```
96+
97+
上面代码中,p1和p2都是Promise的实例,但是p2的resolve方法将p1作为参数,这时p1的状态就会传递给p2。如果调用的时候,p1的状态是pending,那么p2的回调函数就会等待p1的状态改变;如果p1的状态已经是fulfilled或者rejected,那么p2的回调函数将会立刻执行。
98+
99+
## Promise.prototype.then方法:链式操作
100+
101+
Promise.prototype.then方法返回的是一个新的Promise对象,因此可以采用链式写法。
87102

88103
```javascript
89104

@@ -111,9 +126,9 @@ getJSON("/post/1.json").then(function(post) {
111126

112127
这种设计使得嵌套的异步操作,可以被很容易得改写,从回调函数的“横向发展”改为“向下发展”。
113128

114-
## catch方法:捕捉错误
129+
## Promise.prototype.catch方法:捕捉错误
115130

116-
catch方法是then(null, rejection)的别名,用于指定发生错误时的回调函数。
131+
Promise.prototype.catch方法是Promise.prototype.then(null, rejection)的别名,用于指定发生错误时的回调函数。
117132

118133
```javascript
119134

@@ -126,7 +141,7 @@ getJSON("/posts.json").then(function(posts) {
126141

127142
```
128143

129-
Promise对象的错误具有“冒泡”性质,会一直向后传递,直到被捕获为止。
144+
Promise对象的错误具有“冒泡”性质,会一直向后传递,直到被捕获为止。也就是说,错误总是会被下一个catch语句捕获。
130145

131146
```javascript
132147

@@ -140,9 +155,25 @@ getJSON("/post/1.json").then(function(post) {
140155

141156
```
142157

143-
## Promise.all方法
158+
## Promise.all方法,Promise.race方法
159+
160+
Promise.all方法用于将多个Promise实例,包装成一个新的Promise实例。
161+
162+
```javascript
163+
164+
var p = Promise.all([p1,p2,p3]);
165+
166+
```
167+
168+
上面代码中,Promise.all方法接受一个数组作为参数,p1、p2、p3都是Promise对象的实例。(Promise.all方法的参数不一定是数组,但是必须具有iterator接口,且返回的每个成员都是Promise实例。)
169+
170+
p的状态由p1、p2、p3决定,分成两种情况。
171+
172+
(1)只有p1、p2、p3的状态都变成fulfilled,p的状态才会变成fulfilled,此时p1、p2、p3的返回值组成一个数组,传递给p的回调函数。
173+
174+
(2)只要p1、p2、p3之中有一个被rejected,p的状态就变成rejected,此时第一个被reject的实例的返回值,会传递给p的回调函数。
144175

145-
Promise.all方法用于将多个异步操作(或Promise对象),包装成一个新的Promise对象。当这些异步操作都完成后,新的Promise对象的状态才会变为fulfilled;只要其中一个异步操作失败,新的Promise对象的状态就会变为rejected
176+
下面是一个具体的例子
146177

147178
```javascript
148179

@@ -159,7 +190,19 @@ Promise.all(promises).then(function(posts) {
159190

160191
```
161192

162-
## Promise.resolve方法
193+
Promise.race方法同样是将多个Promise实例,包装成一个新的Promise实例。
194+
195+
```javascript
196+
197+
var p = Promise.race([p1,p2,p3]);
198+
199+
```
200+
201+
上面代码中,只要p1、p2、p3之中有一个实例率先改变状态,p的状态就跟着改变。那个率先改变的Promise实例的返回值,就传递给p的返回值。
202+
203+
如果Promise.all方法和Promise.race方法的参数,不是Promise实例,就会先调用下面讲到的Promise.resolve方法,将参数转为Promise实例,再进一步处理。
204+
205+
## Promise.resolve方法,Promise.reject方法
163206

164207
有时需要将现有对象转为Promise对象,Promise.resolve方法就起到这个作用。
165208

@@ -171,7 +214,7 @@ var jsPromise = Promise.resolve($.ajax('/whatever.json'));
171214

172215
上面代码将jQuery生成deferred对象,转为一个新的ES6的Promise对象。
173216

174-
如果Promise.resolve方法的参数,不是具有then方法的对象(又称thenable对象),则返回一个新的Promise对象,且它的状态为resolved
217+
如果Promise.resolve方法的参数,不是具有then方法的对象(又称thenable对象),则返回一个新的Promise对象,且它的状态为fulfilled
175218

176219
```javascript
177220

@@ -184,7 +227,24 @@ p.then(function (s){
184227

185228
```
186229

187-
上面代码生成一个新的Promise对象,它的状态为fulfilled,所以回调函数会立即执行,Promise.resolve方法的参数就是回调函数的参数。
230+
上面代码生成一个新的Promise对象的实例p,它的状态为fulfilled,所以回调函数会立即执行,Promise.resolve方法的参数就是回调函数的参数。
231+
232+
如果Promise.resolve方法的参数是一个Promise对象的实例,则会被原封不动地返回。
233+
234+
Promise.reject(reason)方法也会返回一个新的Promise实例,该实例的状态为rejected。Promise.reject方法的参数reason,会被传递给实例的回调函数。
235+
236+
```javascript
237+
238+
var p = Promise.reject('出错了');
239+
240+
p.then(null, function (s){
241+
console.log(s)
242+
});
243+
// 出错了
244+
245+
```
246+
247+
上面代码生成一个Promise对象的实例p,状态为rejected,回调函数会立即执行。
188248

189249
## async函数
190250

docs/reference.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@
4040
- Marc Harter, [Generators in Node.js: Common Misconceptions and Three Good Use Cases](http://strongloop.com/strongblog/how-to-generators-node-js-yield-use-cases/): 讨论Generator函数的作用
4141
- Axel Rauschmayer, [Iterators and generators in ECMAScript 6](http://www.2ality.com/2013/06/iterators-generators.html): 探讨Iterator和Generator的设计目的
4242
- StackOverflow, [ES6 yield : what happens to the arguments of the first call next()?](http://stackoverflow.com/questions/20977379/es6-yield-what-happens-to-the-arguments-of-the-first-call-next): 第一次使用next方法时不能带有参数
43-
- Kyle Simpson, [ES6 Generators: Complete Series]: 由浅入深探讨Generator的系列文章,共四篇
43+
- Kyle Simpson, [ES6 Generators: Complete Series](http://davidwalsh.name/es6-generators): 由浅入深探讨Generator的系列文章,共四篇
4444

4545
## Promise对象
4646

4747
- Jake Archibald, [JavaScript Promises: There and back again](http://www.html5rocks.com/en/tutorials/es6/promises/)
4848
- Tilde, [rsvp.js](https://github.com/tildeio/rsvp.js)
4949
- Sandeep Panda, [An Overview of JavaScript Promises](http://www.sitepoint.com/overview-javascript-promises/): ES6 Promise入门介绍
5050
- Jafar Husain, [Async Generators](https://docs.google.com/file/d/0B4PVbLpUIdzoMDR5dWstRllXblU/view?sle=true): 对async与Generator混合使用的一些讨论
51+
- Axel Rauschmayer, [ECMAScript 6 promises (2/2): the API](http://www.2ality.com/2014/10/es6-promises-api.html): 对ES6 Promise规格和用法的详细介绍
5152

5253
## 工具
5354

0 commit comments

Comments
 (0)