mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-28 21:32:20 +00:00
edit generator
This commit is contained in:
parent
1352e36b18
commit
9638862076
19
docs/async.md
Normal file
19
docs/async.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# 异步操作
|
||||||
|
|
||||||
|
## co函数库
|
||||||
|
|
||||||
|
如果并发执行异步操作,可以将异步操作都放入一个数组,跟在yield语句后面。
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
co(function* () {
|
||||||
|
var values = [n1, n2, n3];
|
||||||
|
yield values.map(somethingAsync);
|
||||||
|
});
|
||||||
|
|
||||||
|
function* somethingAsync(x) {
|
||||||
|
// do something async
|
||||||
|
return y
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
上面的代码允许并发三个somethingAsync异步操作,等到它们全部完成,才会进行下一步。
|
@ -520,7 +520,39 @@ for(let value of delegatingIterator) {
|
|||||||
|
|
||||||
上面代码中,delegatingIterator是代理者,delegatedIterator是被代理者。由于`yield* delegatedIterator`语句得到的值,是一个遍历器,所以要用星号表示。运行结果就是使用一个遍历器,遍历了多个Genertor函数,有递归的效果。
|
上面代码中,delegatingIterator是代理者,delegatedIterator是被代理者。由于`yield* delegatedIterator`语句得到的值,是一个遍历器,所以要用星号表示。运行结果就是使用一个遍历器,遍历了多个Genertor函数,有递归的效果。
|
||||||
|
|
||||||
如果`yield*`后面跟着一个数组,就表示该数组会返回一个遍历器,因此就会遍历数组成员。
|
再来看一个对比的例子。
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
function* inner() {
|
||||||
|
yield 'hello!'
|
||||||
|
}
|
||||||
|
|
||||||
|
function* outer1() {
|
||||||
|
yield 'open'
|
||||||
|
yield inner()
|
||||||
|
yield 'close'
|
||||||
|
}
|
||||||
|
|
||||||
|
var gen = outer1()
|
||||||
|
gen.next() // -> 'open'
|
||||||
|
gen.next() // -> a generator
|
||||||
|
gen.next() // -> 'close'
|
||||||
|
|
||||||
|
function* outer2() {
|
||||||
|
yield 'open'
|
||||||
|
yield* inner()
|
||||||
|
yield 'close'
|
||||||
|
}
|
||||||
|
|
||||||
|
var gen = outer2()
|
||||||
|
gen.next() // -> 'open'
|
||||||
|
gen.next() // -> 'hello!'
|
||||||
|
gen.next() // -> 'close'
|
||||||
|
```
|
||||||
|
|
||||||
|
上面例子中,outer2使用了`yield*`,outer1没使用。结果就是,outer1返回一个遍历器,outer2返回该遍历器的内部值。
|
||||||
|
|
||||||
|
如果`yield*`后面跟着一个数组,由于数组原生支持遍历器,因此就会遍历数组成员。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
|
||||||
@ -539,16 +571,16 @@ gen().next() // { value:"a", done:false }
|
|||||||
```javascript
|
```javascript
|
||||||
|
|
||||||
function *foo() {
|
function *foo() {
|
||||||
yield 2;
|
yield 2;
|
||||||
yield 3;
|
yield 3;
|
||||||
return "foo";
|
return "foo";
|
||||||
}
|
}
|
||||||
|
|
||||||
function *bar() {
|
function *bar() {
|
||||||
yield 1;
|
yield 1;
|
||||||
var v = yield *foo();
|
var v = yield *foo();
|
||||||
console.log( "v: " + v );
|
console.log( "v: " + v );
|
||||||
yield 4;
|
yield 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
var it = bar();
|
var it = bar();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user