• 前端基础之《ECMAScript 6(7)—异步技术》


    一、生成器

    1、生成器函数是ES6提供的一种异步编程解决方案,语法行为与传统函数完全不同

    2、生成器其实是一个特殊的函数,用于异步编程

    3、以前用的方式都是纯回调函数

    比如:node中fs模块,ajax

    4、生成器的声明和调用

    function+星号+函数名

    1. function * gen(){
    2. console.log("hello generator");
    3. }
    4. let iterator = gen();
    5. //打印这个变量
    6. console.log(iterator);
    7. //让代码运行,用next方法执行
    8. iterator.next(); //hello generator

    可以看到打印出的对象里有next方法。用next方法执行函数。

    5、生成器函数可以有yield语句

    yield后面跟表达式或者自变量。

    1. function * gen2(){
    2. console.log(111);
    3. yield '一只没有耳朵';
    4. console.log(222);
    5. yield '一只没有尾巴';
    6. console.log(333);
    7. yield '真奇怪';
    8. console.log(444);
    9. }
    10. let iterator2 = gen2();
    11. iterator2.next();
    12. iterator2.next();
    13. iterator2.next();
    14. iterator2.next();

    6、yield是函数代码的分隔符

    调一个next,执行一段函数。

    7、用for...of遍历对象

    1. //for...of遍历
    2. for(let v of gen2()){
    3. console.log(v); //输出的值就是yield后的值
    4. }

    8、next()方法的执行返回结果

    返回yield后边,这个语句或者自变量的值。

    1. function * gen3(){
    2. yield 111;
    3. yield 222;
    4. yield 333;
    5. }
    6. //执行获取迭代器对象
    7. let iterator3 = gen3();
    8. console.log(iterator3.next());
    9. console.log(iterator3.next());
    10. console.log(iterator3.next());
    11. console.log(iterator3.next());

    二、生成器函数参数

    1、生成器传参

    往生成器传参数,只有调用next()才能执行代码获取。

    2、next传参

    第二次调用next()传入的参数,作为第一个yield返回的结果,以此类推。

    1. function * gen4(args){
    2. console.log(args);
    3. let one = yield 111;
    4. console.log(one);
    5. let two = yield 222;
    6. console.log(two);
    7. let three = yield 333;
    8. console.log(three);
    9. }
    10. //执行获取迭代器对象
    11. let iterator4 = gen4('AAA');
    12. console.log(iterator4.next());
    13. //next方法可以传入实参
    14. console.log(iterator4.next('BBB'));
    15. console.log(iterator4.next('CCC'));
    16. console.log(iterator4.next('DDD'));

    三、生成器函数实例

    1、js本生是单线程的,异步单线程。所以很多操作是异步完成的。比如:IO操作,文件操作,网络操作,数据库操作

    2、定时器需求

    1s后控制台输出111,2s后输出222,3s后输出333,总共6s。

    1. function one(){
    2. setTimeout(()=>{
    3. console.log(111);
    4. iterator.next();
    5. }, 1000);
    6. }
    7. function two(){
    8. setTimeout(()=>{
    9. console.log(222);
    10. iterator.next();
    11. }, 2000);
    12. }
    13. function three(){
    14. setTimeout(()=>{
    15. console.log(333);
    16. iterator.next();
    17. }, 3000);
    18. }
    19. function * gen(){
    20. yield one();
    21. yield two();
    22. yield three();
    23. }
    24. //调用生成器函数
    25. let iterator = gen();
    26. iterator.next();

    3、模拟获取数据

    1s后获取用户数据,1s后获取订单数据,1s后商品数据。

    1. function getUsers(){
    2. setTimeout(()=>{
    3. let data = '用户数据';
    4. //调用next方法,并且将数据传入
    5. //它的实参,将作为第一个yield语句的返回结果
    6. iterator2.next(data);
    7. }, 1000);
    8. }
    9. function getOrders(){
    10. setTimeout(()=>{
    11. let data = '订单数据';
    12. iterator2.next(data);
    13. }, 1000);
    14. }
    15. function getGoods(){
    16. setTimeout(()=>{
    17. let data = '商品数据';
    18. iterator2.next(data);
    19. }, 1000);
    20. }
    21. function * gen2(){
    22. let users = yield getUsers();
    23. console.log(users);
    24. let orders = yield getOrders();
    25. console.log(orders);
    26. let goods = yield getGoods();
    27. console.log(goods);
    28. }
    29. //调用生成器函数
    30. let iterator2 = gen2();
    31. iterator2.next();

  • 相关阅读:
    Qt QImag图像保存、格式转换
    WebService总结
    KVM虚拟化进阶--KVM设备高级管理
    FGH40N60SMD安森美车规IGBT,ASEMI原厂代理FGH40N60SMD
    稀疏数组举例详解(Java形式表示)
    Python类的疑难点
    美军2分钟快速入睡法
    [操作系统笔记]基本分页存储管理
    SpringBoot启动失败报错,spring.profiles.active:@env@中环境变量@无法识别报错_active: @env@
    《Envoy 代理:云原生时代的流量管理》
  • 原文地址:https://blog.csdn.net/csj50/article/details/127426257