mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 10:22:23 +00:00
修改promise
This commit is contained in:
parent
4bba9a0a71
commit
032e950fdd
@ -10,9 +10,9 @@
|
||||
|
||||
全书已由电子工业出版社出版([版权页](images/copyright.png),[内页1](images/page1.png),[内页2](images/page2.png)),铜版纸全彩印刷,附有索引。欢迎大家购买,支持开源项目。
|
||||
|
||||
- [亚马逊](http://www.amazon.cn/%E5%9B%BE%E4%B9%A6/dp/B00MQKRLD6/)
|
||||
- [京东](http://item.jd.com/11526272.html)
|
||||
- [当当](http://product.dangdang.com/23546442.html)
|
||||
- [亚马逊](http://www.amazon.cn/%E5%9B%BE%E4%B9%A6/dp/B00MQKRLD6/)
|
||||
- [China-pub](http://product.china-pub.com/4284817)
|
||||
|
||||
### 版权许可
|
||||
|
@ -60,13 +60,11 @@ var getJSON = function(url) {
|
||||
client.send();
|
||||
|
||||
function handler() {
|
||||
if (this.readyState === this.DONE) {
|
||||
if (this.status === 200) {
|
||||
resolve(this.response);
|
||||
} else {
|
||||
reject(this);
|
||||
}
|
||||
}
|
||||
if (this.status === 200) {
|
||||
resolve(this.response);
|
||||
} else {
|
||||
reject(new Error(this.statusText));
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
@ -74,16 +72,33 @@ var getJSON = function(url) {
|
||||
};
|
||||
|
||||
getJSON("/posts.json").then(function(json) {
|
||||
// continue
|
||||
console.log('Contents: ' + value);
|
||||
}, function(error) {
|
||||
// handle errors
|
||||
console.error('出错了', reason);
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
## 链式操作
|
||||
上面代码中,resolve方法和reject方法调用时,都带有参数。它们的参数会被传递给回调函数。reject方法的参数通常是Error对象的实例,而resolve方法的参数除了正常的值以外,还可能是另一个Promise实例,比如像下面这样。
|
||||
|
||||
then方法返回的是一个新的Promise对象,因此可以采用链式写法。
|
||||
```javascript
|
||||
|
||||
var p1 = new Promise(function(resolve, reject){
|
||||
// ... some code
|
||||
});
|
||||
|
||||
var p2 = new Promise(function(resolve, reject){
|
||||
// ... some code
|
||||
resolve(p1);
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
上面代码中,p1和p2都是Promise的实例,但是p2的resolve方法将p1作为参数,这时p1的状态就会传递给p2。如果调用的时候,p1的状态是pending,那么p2的回调函数就会等待p1的状态改变;如果p1的状态已经是fulfilled或者rejected,那么p2的回调函数将会立刻执行。
|
||||
|
||||
## Promise.prototype.then方法:链式操作
|
||||
|
||||
Promise.prototype.then方法返回的是一个新的Promise对象,因此可以采用链式写法。
|
||||
|
||||
```javascript
|
||||
|
||||
@ -111,9 +126,9 @@ getJSON("/post/1.json").then(function(post) {
|
||||
|
||||
这种设计使得嵌套的异步操作,可以被很容易得改写,从回调函数的“横向发展”改为“向下发展”。
|
||||
|
||||
## catch方法:捕捉错误
|
||||
## Promise.prototype.catch方法:捕捉错误
|
||||
|
||||
catch方法是then(null, rejection)的别名,用于指定发生错误时的回调函数。
|
||||
Promise.prototype.catch方法是Promise.prototype.then(null, rejection)的别名,用于指定发生错误时的回调函数。
|
||||
|
||||
```javascript
|
||||
|
||||
@ -126,7 +141,7 @@ getJSON("/posts.json").then(function(posts) {
|
||||
|
||||
```
|
||||
|
||||
Promise对象的错误具有“冒泡”性质,会一直向后传递,直到被捕获为止。
|
||||
Promise对象的错误具有“冒泡”性质,会一直向后传递,直到被捕获为止。也就是说,错误总是会被下一个catch语句捕获。
|
||||
|
||||
```javascript
|
||||
|
||||
@ -140,9 +155,25 @@ getJSON("/post/1.json").then(function(post) {
|
||||
|
||||
```
|
||||
|
||||
## Promise.all方法
|
||||
## Promise.all方法,Promise.race方法
|
||||
|
||||
Promise.all方法用于将多个异步操作(或Promise对象),包装成一个新的Promise对象。当这些异步操作都完成后,新的Promise对象的状态才会变为fulfilled;只要其中一个异步操作失败,新的Promise对象的状态就会变为rejected。
|
||||
Promise.all方法用于将多个Promise实例,包装成一个新的Promise实例。
|
||||
|
||||
```javascript
|
||||
|
||||
var p = Promise.all([p1,p2,p3]);
|
||||
|
||||
```
|
||||
|
||||
上面代码中,Promise.all方法接受一个数组作为参数,p1、p2、p3都是Promise对象的实例。(Promise.all方法的参数不一定是数组,但是必须具有iterator接口,且返回的每个成员都是Promise实例。)
|
||||
|
||||
p的状态由p1、p2、p3决定,分成两种情况。
|
||||
|
||||
(1)只有p1、p2、p3的状态都变成fulfilled,p的状态才会变成fulfilled,此时p1、p2、p3的返回值组成一个数组,传递给p的回调函数。
|
||||
|
||||
(2)只要p1、p2、p3之中有一个被rejected,p的状态就变成rejected,此时第一个被reject的实例的返回值,会传递给p的回调函数。
|
||||
|
||||
下面是一个具体的例子。
|
||||
|
||||
```javascript
|
||||
|
||||
@ -159,7 +190,19 @@ Promise.all(promises).then(function(posts) {
|
||||
|
||||
```
|
||||
|
||||
## Promise.resolve方法
|
||||
Promise.race方法同样是将多个Promise实例,包装成一个新的Promise实例。
|
||||
|
||||
```javascript
|
||||
|
||||
var p = Promise.race([p1,p2,p3]);
|
||||
|
||||
```
|
||||
|
||||
上面代码中,只要p1、p2、p3之中有一个实例率先改变状态,p的状态就跟着改变。那个率先改变的Promise实例的返回值,就传递给p的返回值。
|
||||
|
||||
如果Promise.all方法和Promise.race方法的参数,不是Promise实例,就会先调用下面讲到的Promise.resolve方法,将参数转为Promise实例,再进一步处理。
|
||||
|
||||
## Promise.resolve方法,Promise.reject方法
|
||||
|
||||
有时需要将现有对象转为Promise对象,Promise.resolve方法就起到这个作用。
|
||||
|
||||
@ -171,7 +214,7 @@ var jsPromise = Promise.resolve($.ajax('/whatever.json'));
|
||||
|
||||
上面代码将jQuery生成deferred对象,转为一个新的ES6的Promise对象。
|
||||
|
||||
如果Promise.resolve方法的参数,不是具有then方法的对象(又称thenable对象),则返回一个新的Promise对象,且它的状态为resolved。
|
||||
如果Promise.resolve方法的参数,不是具有then方法的对象(又称thenable对象),则返回一个新的Promise对象,且它的状态为fulfilled。
|
||||
|
||||
```javascript
|
||||
|
||||
@ -184,7 +227,24 @@ p.then(function (s){
|
||||
|
||||
```
|
||||
|
||||
上面代码生成一个新的Promise对象,它的状态为fulfilled,所以回调函数会立即执行,Promise.resolve方法的参数就是回调函数的参数。
|
||||
上面代码生成一个新的Promise对象的实例p,它的状态为fulfilled,所以回调函数会立即执行,Promise.resolve方法的参数就是回调函数的参数。
|
||||
|
||||
如果Promise.resolve方法的参数是一个Promise对象的实例,则会被原封不动地返回。
|
||||
|
||||
Promise.reject(reason)方法也会返回一个新的Promise实例,该实例的状态为rejected。Promise.reject方法的参数reason,会被传递给实例的回调函数。
|
||||
|
||||
```javascript
|
||||
|
||||
var p = Promise.reject('出错了');
|
||||
|
||||
p.then(null, function (s){
|
||||
console.log(s)
|
||||
});
|
||||
// 出错了
|
||||
|
||||
```
|
||||
|
||||
上面代码生成一个Promise对象的实例p,状态为rejected,回调函数会立即执行。
|
||||
|
||||
## async函数
|
||||
|
||||
|
@ -40,7 +40,7 @@
|
||||
- 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函数的作用
|
||||
- Axel Rauschmayer, [Iterators and generators in ECMAScript 6](http://www.2ality.com/2013/06/iterators-generators.html): 探讨Iterator和Generator的设计目的
|
||||
- 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方法时不能带有参数
|
||||
- Kyle Simpson, [ES6 Generators: Complete Series]: 由浅入深探讨Generator的系列文章,共四篇
|
||||
- Kyle Simpson, [ES6 Generators: Complete Series](http://davidwalsh.name/es6-generators): 由浅入深探讨Generator的系列文章,共四篇
|
||||
|
||||
## Promise对象
|
||||
|
||||
@ -48,6 +48,7 @@
|
||||
- Tilde, [rsvp.js](https://github.com/tildeio/rsvp.js)
|
||||
- Sandeep Panda, [An Overview of JavaScript Promises](http://www.sitepoint.com/overview-javascript-promises/): ES6 Promise入门介绍
|
||||
- Jafar Husain, [Async Generators](https://docs.google.com/file/d/0B4PVbLpUIdzoMDR5dWstRllXblU/view?sle=true): 对async与Generator混合使用的一些讨论
|
||||
- Axel Rauschmayer, [ECMAScript 6 promises (2/2): the API](http://www.2ality.com/2014/10/es6-promises-api.html): 对ES6 Promise规格和用法的详细介绍
|
||||
|
||||
## 工具
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user