es6中的生成器generator

特点:
①function关键字与函数名之间有个星号*;
②函数体内部使用yield表达式,定义不同的内部状态(yield“产出”)

function* helloWorldGenerator () {
       yield "hello";
        yield "world";
         return "ending";  
}
var hw = helloworldGenerator();
hw.next()
// { value: 'hello', done: false }

hw.next()
// { value: 'world', done: false }

hw.next()
// { value: 'ending', done: true }

hw.next()
// { value: undefined, done: true }

第一次调用,Generator 函数开始执行,直到遇到第一个yield表达式为止。next方法返回一个对象,它的value属性就是当前yield表达式的值hello,done属性的值false,表示遍历还没有结束。

第二次调用,Generator 函数从上次yield表达式停下的地方,一直执行到下一个yield表达式。next方法返回的对象的value属性就是当前yield表达式的值world,done属性的值false,表示遍历还没有结束。

第三次调用,Generator 函数从上次yield表达式停下的地方,一直执行到return语句(如果没有return语句,就执行到函数结束)。next方法返回的对象的value属性,就是紧跟在return语句后面的表达式的值(如果没有return语句,则value属性的值为undefined),done属性的值true,表示遍历已经结束。

第四次调用,此时 Generator 函数已经运行完毕,next方法返回对象的value属性为undefined,done属性为true。以后再调用next方法,返回的都是这个值。

function* fibs() {
    let a = 0;
    let b = 2;
    while (true) {
        yield a;
        [a, b] = [b, a + b];
    }
}
let [first, second, third, fourth, fifth, sixth] = fibs();
//上来先执行自己的a=0,然后再累加
console.log(first,second,third,fourth,fifth,sixth)   //0 2 2 4 6 10

应用:通过generator进行异步函数调用

function one() {
    setTimeout(()=>{
        console.log(111);
        iterator.next();
    },1000)
}

function two() {
    setTimeout(()=>{
        console.log(222);
        iterator.next();
    },2000)
}
function three() {
    setTimeout(()=>{
        console.log(333);
        iterator.next();
    },3000)
}
function* gen() {
    yield one();
    yield two();
    yield three();
}
//调用生成器函数
let iterator=gen();
iterator.next();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值