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

docs(async): edit async/asyncIterable

This commit is contained in:
ruanyf 2017-01-01 19:21:09 +08:00
parent a44b2f0fa8
commit f02924cd5a
2 changed files with 30 additions and 16 deletions

View File

@ -1323,9 +1323,9 @@ async function logInOrder(urls) {
## 异步遍历器
《遍历器》一章说过Iterator 接口是一种数据遍历的协议,只要调用遍历器对象的`next`方法,就会得到一个表示当前成员信息的对象`{value, done}`。其中,`value`表示当前的数据的值,`done`是一个布尔值,表示遍历是否结束。
《遍历器》一章说过Iterator 接口是一种数据遍历的协议,只要调用遍历器对象的`next`方法,就会得到一个对象,表示当前遍历指针所在的那个位置的信息。`next`方法返回的对象的结构是`{value, done}`,其中`value`表示当前的数据的值,`done`是一个布尔值,表示遍历是否结束。
这隐含着规定,`next`方法是同步的,只要调用就必须立刻返回值。也就是说,一旦执行`next`方法,就必须同步地得到`value``done`这两方面的信息。这对于同步操作当然没有问题但对于异步操作就不太合适了。目前的解决方法是Generator函数里面的异步操作返回一个Thunk函数或者Promise对象`value`属性是一个Thunk函数或者Promise对象等待以后返回真正的值`done`属性则还是同步产生的。
隐含着一个规定,`next`方法必须是同步的,只要调用就必须立刻返回值。也就是说,一旦执行`next`方法,就必须同步地得到`value``done`这两个属性。如果遍历指针正好指向同步操作当然没有问题但对于异步操作就不太合适了。目前的解决方法是Generator 函数里面的异步操作,返回一个 Thunk 函数或者 Promise 对象,即`value`属性是一个 Thunk 函数或者 Promise 对象,等待以后返回真正的值,而`done`属性则还是同步产生的。
目前,有一个[提案](https://github.com/tc39/proposal-async-iteration),为异步操作提供原生的遍历器接口,即`value``done`这两个属性都是异步产生这称为”异步遍历器“Async Iterator
@ -1351,14 +1351,17 @@ asyncIterator
const asyncIterable = createAsyncIterable(['a', 'b']);
const asyncIterator = someCollection[Symbol.asyncIterator]();
asyncIterator.next()
asyncIterator
.next()
.then(iterResult1 => {
console.log(iterResult1); // { value: 'a', done: false }
return asyncIterator.next();
}).then(iterResult2 => {
})
.then(iterResult2 => {
console.log(iterResult2); // { value: 'b', done: false }
return asyncIterator.next();
}).then(iterResult3 => {
})
.then(iterResult3 => {
console.log(iterResult3); // { value: undefined, done: true }
});
```
@ -1418,6 +1421,17 @@ async function f() {
上面代码中,`createAsyncIterable()`返回一个异步遍历器,`for...of`循环自动调用这个遍历器的`next`方法会得到一个Promise对象。`await`用来处理这个Promise对象一旦`resolve`,就把得到的值(`x`)传入`for...of`的循环体。
`for await...of`循环的一个用途,是部署了 asyncIterable 操作的异步接口,可以直接放入这个循环。
```javascript
let body = '';
for await(const data on req) body += data;
const parsed = JSON.parse(body);
console.log("got", parsed);
```
上面代码中,`req`是一个 asyncIterable 对象,用来异步读取数据。可以看到,使用`for await...of`循环以后,代码会非常简洁。
如果`next`方法返回的Promise对象被`reject`,那么就要用`try...catch`捕捉。
```javascript

View File

@ -707,7 +707,7 @@ g.next() // { done: true, value: 7 }
## yield* 语句
如果在Generater函数内部调用另一个Generator函数,默认情况下是没有效果的。
如果在 Generator 函数内部,调用另一个 Generator 函数,默认情况下是没有效果的。
```javascript
function* foo() {