ES6中Generator理解

Stella981
• 阅读 571

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

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Stella981 Stella981
2年前
JS 对象数组Array 根据对象object key的值排序sort,很风骚哦
有个js对象数组varary\{id:1,name:"b"},{id:2,name:"b"}\需求是根据name或者id的值来排序,这里有个风骚的函数函数定义:function keysrt(key,desc) {  return function(a,b){    return desc ? ~~(ak
Wesley13 Wesley13
2年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这