1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-24 10:22:23 +00:00

修改promise

This commit is contained in:
ruanyf 2014-10-06 14:52:05 +08:00
parent 4bba9a0a71
commit 032e950fdd
3 changed files with 82 additions and 21 deletions

View File

@ -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)
### 版权许可

View File

@ -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的状态都变成fulfilledp的状态才会变成fulfilled此时p1、p2、p3的返回值组成一个数组传递给p的回调函数。
2只要p1、p2、p3之中有一个被rejectedp的状态就变成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函数

View File

@ -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规格和用法的详细介绍
## 工具