ES6中Generator理解

1. 生成器函数声明

   function*  name(args){};

2. yield使用

function* hello(){     console.log('before hello');  //可看到hello()并不会立刻执行函数, 到第一次next调用时才会     var name = yield 'please input your name';  //将字符串传递给第一次next的结果, 并等待接收第二次next传过来的数据.     yield 'hello : ' + name;  //返回结果给外部的next     console.log('after hello'); } var helloer = hello();  //返回生成器实例 console.log(helloer.next());  //开始执行函数, 碰到第一个yield时返回 console.log(helloer.next('haogrgr'));  //接着第一个yield开始执行, 知道第二个yield, 这里通过next传递了参数. console.log(helloer.next());  //继续执行console.log('after hello');, 并返回done:true, 表示执行完了. //输出 before hello Object {value: "please input your name", done: false}   //第一次next调用输出到这里 Object {value: "hello : haogrgr", done: false}                  //第二次next调用输出到这里 after hello                                                                           Object {value: undefined, done: true}                             //第三次next调用输出到这里

   真实的执行顺序大概为:

1)创建generator实例. 2)第一次调用next方法, 开始执行函数体 第一次 next{     console.log('before hello');     return  'please input your name';  //碰到yield, 返回yield后面的值 } 3)next调用返回.   4)console输出next的返回值  Object {value: "please input your name", done: false} 5)第二次调用next方法, 并传入参数  'haogrgr'. 第二次next{     var name = 'haogrgr'; //haogrgr通过 next('haogrgr')传过来     return  'hello : ' + name;  //碰到yield, 返回 } 6)console输出next的返回值  Object {value: "hello : haogrgr", done: false}  7)第三次调用next 第三次next{     console.log('after hello'); } 8)console输出next的返回值  Object {value: undefined, done: true}

3. 总结

1)申明生成器函数使用  function* 开头.

2)调用生成器函数生成新的生成器实例.

3)第一次调用生成器next方法, 会开始执行函数, 直到碰到yield关键字.

4)yield关键字后面可以返回一个值给next调用.

5)同时, next方法可以有参数, 参数的值会作为   (yield exp)   的值.

6)yield和next可以看作是通过消息交流的两个组件,  比如 (yield msg) 传递一个消息给next, 然后暂停, 等待下个一个next调用传递一个值作为(yield msg)的返回值.

7)生成器函数中, return的值, 会作为最后一次next调用的返回值的value属性.

8)异常可以通过helloer.throw(err)来抛出.

4. 一种实现方式

1)转义, 比如facebook出的工具regenerator,   对于上面的例子, 会转换为如下的代码.

var marked0$0 = [hello].map(regeneratorRuntime.mark); function hello() {     var name;     return regeneratorRuntime.wrap(function hello$(context$1$0) {         while (1) switch (context$1$0.prev = context$1$0.next) {         case 0:             console.log('before hello');             context$1$0.next = 3;             return 'please input your name';         case 3:             name = context$1$0.sent;             context$1$0.next = 6;             return 'hello : ' + name;         case 6:             console.log('after hello');         case 7:         case "end":             return context$1$0.stop();         }     }, marked0$0[0], this); } var helloer = hello(); console.log(helloer.next()); console.log(helloer.next('haogrgr')); console.log(helloer.next());

可以看到, 通过语法转换, 然后加上一个运行时, 成功了实现了生成器的功能.

通过将函数逻辑分割, 然后再上下文中记录当前的步骤, 来达到控制的转移(原本一路执行到底的函数变成了, 一节一节的跳来跳去的执行).

5. 参考资料

http://www.html5rocks.com/zh/tutorials/es6/promises/

http://www.infoq.com/cn/articles/es6-in-depth-generators

http://www.infoq.com/cn/articles/es6-in-depth-generators-continued

https://facebook.github.io/regenerator/

http://www.zhihu.com/question/30820791

http://swannodette.github.io/2013/08/24/es6-generators-and-csp/

http://swannodette.github.io/2013/07/31/extracting-processes/

http://www.zhihu.com/question/30820791

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang