mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 18:32:22 +00:00
edit Promise
This commit is contained in:
parent
d12c9c843f
commit
de33b8cca6
@ -408,7 +408,7 @@ someAsyncThing().then(function() {
|
||||
var p = Promise.all([p1, p2, p3]);
|
||||
```
|
||||
|
||||
上面代码中,`Promise.all`方法接受一个数组作为参数,`p1`、`p2`、`p3`都是Promise对象的实例,如果不是,就会先调用下面讲到的`Promise.resolve`方法,将参数转为Promise实例,再进一步处理。(`Promise.all`方法的参数不一定是数组,但是必须具有iterator接口,且返回的每个成员都是Promise实例。)
|
||||
上面代码中,`Promise.all`方法接受一个数组作为参数,`p1`、`p2`、`p3`都是Promise对象的实例,如果不是,就会先调用下面讲到的`Promise.resolve`方法,将参数转为Promise实例,再进一步处理。(`Promise.all`方法的参数可以不是数组,但必须具有Iterator接口,且返回的每个成员都是Promise实例。)
|
||||
|
||||
`p`的状态由`p1`、`p2`、`p3`决定,分成两种情况。
|
||||
|
||||
@ -430,6 +430,29 @@ Promise.all(promises).then(function(posts) {
|
||||
// ...
|
||||
});
|
||||
```
|
||||
|
||||
上面代码中,`promises`是包含6个Promise实例的数组,只有这6个实例的状态都变成`fulfilled`,或者其中有一个变为`rejected`,才会调用`Promise.all`方法后面的回调函数。
|
||||
|
||||
下面是另一个例子。
|
||||
|
||||
```javascript
|
||||
const databasePromise = connectDatabase();
|
||||
|
||||
const booksPromise = databaseProimse
|
||||
.then(findAllBooks);
|
||||
|
||||
const userPromise = databasePromise
|
||||
.then(getCurrentUser);
|
||||
|
||||
Promise.all([
|
||||
booksPromise,
|
||||
userPromise
|
||||
])
|
||||
.then(([books, user]) => pickTopRecommentations(books, user));
|
||||
```
|
||||
|
||||
上面代码中,`booksPromise`和`userPromise`是两个异步操作,只有等到它们的结果都返回了,才会触发`pickTopRecommentations`这个回调函数。
|
||||
|
||||
## Promise.race()
|
||||
|
||||
`Promise.race`方法同样是将多个Promise实例,包装成一个新的Promise实例。
|
||||
|
@ -143,6 +143,7 @@
|
||||
- Steven Sanderson, [Experiments with Koa and JavaScript Generators](http://blog.stevensanderson.com/2013/12/21/experiments-with-koa-and-javascript-generators/): Generator入门介绍,以Koa框架为例
|
||||
- Mahdi Dibaiee, [ES7 Array and Generator comprehensions](http://dibaiee.ir/es7-array-generator-comprehensions/):ES7的Generator推导
|
||||
- Nicolas Bevacqua, [ES6 Generators in Depth](http://ponyfoo.com/articles/es6-generators-in-depth)
|
||||
- Axel Rauschmayer, [ES6 generators in depth](http://www.2ality.com/2015/03/es6-generators.html): Generator规格的详尽讲解
|
||||
|
||||
## Promise对象
|
||||
|
||||
@ -150,13 +151,16 @@
|
||||
- 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入门介绍
|
||||
- Dave Atchley, [ES6 Promises](http://www.datchley.name/es6-promises/): 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规格和用法的详细介绍
|
||||
- Jack Franklin, [Embracing Promises in JavaScript](http://javascriptplayground.com/blog/2015/02/promises/): catch方法的例子
|
||||
- Ronald Chen, [How to escape Promise Hell](https://medium.com/@pyrolistical/how-to-get-out-of-promise-hell-8c20e0ab0513#.2an1he6vf): 如何使用`Promise.all`方法的一些很好的例子
|
||||
|
||||
## 异步操作和Async函数
|
||||
|
||||
- Luke Hoban, [Async Functions for ECMAScript](https://github.com/lukehoban/ecmascript-asyncawait): Async函数的设计思想,与Promise、Gernerator函数的关系
|
||||
- Jafar Husain, [Asynchronous Generators for ES7](https://github.com/jhusain/asyncgenerator): Async函数的深入讨论
|
||||
- Nolan Lawson, [Taming the asynchronous beast with ES7](http://pouchdb.com/2015/03/05/taming-the-async-beast-with-es7.html): async函数通俗的实例讲解
|
||||
- Axel Rauschmayer, [ES6 generators in depth](http://www.2ality.com/2015/03/es6-generators.html): Generator规格的详尽讲解
|
||||
- Jafar Husain, [Async Generators](https://docs.google.com/file/d/0B4PVbLpUIdzoMDR5dWstRllXblU/view?sle=true): 对async与Generator混合使用的一些讨论
|
||||
|
||||
## Class
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user