File tree Expand file tree Collapse file tree 1 file changed +6
-6
lines changed Expand file tree Collapse file tree 1 file changed +6
-6
lines changed Original file line number Diff line number Diff line change @@ -420,7 +420,7 @@ ft(1, 2)(print);
420
420
421
421
### Generator 函数的流程管理
422
422
423
- 你可能会问, Thunk函数有什么用 ?回答是以前确实没什么用,但是 ES6 有了 Generator 函数,Thunk 函数现在可以用于 Generator 函数的自动流程管理。
423
+ 你可能会问, Thunk 函数有什么用 ?回答是以前确实没什么用,但是 ES6 有了 Generator 函数,Thunk 函数现在可以用于 Generator 函数的自动流程管理。
424
424
425
425
Generator 函数可以自动执行。
426
426
@@ -544,19 +544,19 @@ var co = require('co');
544
544
co (gen);
545
545
```
546
546
547
- 上面代码中,Generator函数只要传入co函数 ,就会自动执行。
547
+ 上面代码中,Generator函数只要传入 ` co ` 函数 ,就会自动执行。
548
548
549
- co函数返回一个Promise对象,因此可以用then方法添加回调函数 。
549
+ ` co ` 函数返回一个Promise对象,因此可以用 ` then ` 方法添加回调函数 。
550
550
551
551
``` javascript
552
552
co (gen).then (function (){
553
553
console .log (' Generator 函数执行完成' );
554
554
});
555
555
```
556
556
557
- 上面代码中,等到Generator函数执行结束 ,就会输出一行提示。
557
+ 上面代码中,等到 Generator 函数执行结束 ,就会输出一行提示。
558
558
559
- ### co模块的原理
559
+ ### co 模块的原理
560
560
561
561
为什么 co 可以自动执行 Generator 函数?
562
562
@@ -568,7 +568,7 @@ co(gen).then(function (){
568
568
569
569
(2)Promise 对象。将异步操作包装成 Promise 对象,用` then ` 方法交回执行权。
570
570
571
- co 模块其实就是将两种自动执行器(Thunk 函数和 Promise 对象),包装成一个模块。使用 co 的前提条件是,Generator 函数的` yield ` 命令后面,只能是 Thunk 函数或 Promise 对象。
571
+ co 模块其实就是将两种自动执行器(Thunk 函数和 Promise 对象),包装成一个模块。使用 co 的前提条件是,Generator 函数的` yield ` 命令后面,只能是 Thunk 函数或 Promise 对象。如果数组或对象的成员,全部都是 Promise 对象,也是可以的,详见后文的例子。(4.0 版以后, ` yield ` 命令后面只能是 Promise 对象。)
572
572
573
573
上一节已经介绍了基于 Thunk 函数的自动执行器。下面来看,基于 Promise 对象的自动执行器。这是理解 co 模块必须的。
574
574
You can’t perform that action at this time.
0 commit comments