百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>JS教程> ES6 Generator 根本使用
分享文章到:

ES6 Generator 根本使用

发布时间:09/01 来源:未知 浏览: 关键词:

本文实例讲述了ES6 Generator根本使用办法。分享给大家供大家参照 ,详细如下:

1.Generator介绍

先来一段Generator的根基代码

function* g(){
 yield 100;
 yield 200;
 return 300;
}
let gg = g();
console.log(gg);            // Object [Generator] {}
console.log(gg.next());        // { value: 100, done: false }
console.log(gg.next());        // { value: 200, done: false }
console.log(gg.next());        // { value: 300, done: true }
console.log(gg.next());        // { value: undefined, done: true }

第一我们看到:

  • Generator是由functinon*定义的,在generator内部可以使用yield

  • Generator不是函数,而是一个对象,并且在施行开端就进入暂停状态,而不是直接施行全部操纵

  • 通过next()来施行下一步操纵,返回的都是{ value: xxx, done: xxx }这样的情势,value代表上一次操纵返回的值,done有两个值,一个是true,一个是false,表示整个流程可否全部完毕。

2.Generator异步编程

generator是ES6中引入的异步解决方案,我们来看看它与promise处置异步的对照,来看它们的差别。

// 这里模拟了一个异步操纵
function asyncFunc(data) {
 return new Promise( resolve => {
  setTimeout(
   function() {
    resolve(data)
   },1000
  )
 })
}

promise的处置方式:

asyncFunc("abc").then( res => {
 console.log(res);                    // "abc"
 return asyncFunc("def")
}).then( res => {
 console.log(res);                    // "def"
 return asyncFunc("ghi")
}).then( res => {
 console.log(res);                    // "ghi"
})

generator的处置方式:

function* g() {
 const r1 = yield asyncFunc("abc");
 console.log(r1);                            // "abc"
 const r2 = yield asyncFunc("def");
 console.log(r2);                            // "def"
 const r3 = yield asyncFunc("ghi");
 console.log(r3);                            // "ghi"
}

let gg = g();
let r1 = gg.next();
r1.value.then(res => {
 let r2 = gg.next(res);
 r2.value.then(res => {
  let r3 = gg.next(res);
  r3.value.then(res => {
   gg.next(res)
  })
 })
})

promise屡次回调显得比力复杂,代码也不足简约,generator在异步处置上看似同步的代码,实际是异步的操纵,独一就是在处置上会相对复杂,假如只停止一次异步操纵,generator更适宜。

3.yield和yield*

先来看两段代码

function* g1() {
 yield 100;
 yield g2();
 return 400;
}

function* g2() {
 yield 200;
 yield 300;
}

let gg = g1();
console.log(gg.next());                // { value: 100, done: false }
console.log(gg.next());                // { value: Object [Generator] {}, done: false }
console.log(gg.next());                // { value: 400, done: true }
console.log(gg.next());                // { value: undefined, done: true }
function* g1() {
 yield 100;
 yield* g2();
 return 400;
}

function* g2() {
 yield 200;
 yield 300;
}

let gg = g1();
console.log(gg.next());                // { value: 100, done: false }
console.log(gg.next());                // { value: 200, done: false }
console.log(gg.next());                // { value: 300, done: false }
console.log(gg.next());                // { value: 400, done: true }

yield对另一个generator不会停止遍历,返回的是迭代器对象,而yield*会对generator停止遍历迭代。

引荐教程:《JS教程》

以上就是ES6 Generator 根本使用的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有150人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板