Genertor生成器 、Generator内next()的传参问题、 Generator模拟用户下单、async的使用:

28 篇文章 2 订阅

目录

Genertor生成器

 Generator内next()的传参问题

 Generator模拟用户下单

async的使用:


Genertor生成器

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //创建一个Generator函数
      //1.function和函数名之间要有一个*
      //2.函数内部会有yield表达式,使用yield表达式结合next进行使用
      function* gen() {
        console.log("111");
        yield "两只老虎";
        console.log("222");
        yield "跑得快";
        console.log("333");
        yield "跑的快";
        console.log("444");
        yield "一只没有尾巴";
      }
      let a = gen();
      //a.next()要看迭代器内部有多少个状态,就执行几次next()
      console.log(a.next());
      console.log(a.next());
      console.log(a.next());
      console.log(a.next());
      console.log(a.next());
    </script>
  </body>
</html>

 Generator内next()的传参问题

       //1.传参是可以的

      //如果创建Generator函数的时候,有设置形式参数,

      //但是用next()方法调用的时候,没有对应传实际参数,

      //这次调用无效

      //Generator里的next()方法可以进行参数的传递,

      //此参数作为yield表达式的返回值 next优先 yield次之

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //参数传递问题
      //创建一个generator函数
      function* gen(arg) {
        console.log(arg, "参数");

        let one = yield 111;
        console.log("one--", one);

        let two = yield 222;
        console.log("one--", two);

        let three = yield 333;
        console.log("one--", three);
      }
      let a = gen("hello world");
      //1.传参是可以的
      //如果创建Generator函数的时候,有设置形式参数,
      //但是用next()方法调用的时候,没有对应传实际参数,
      //这次调用无效
      a.next();
      //Generator里的next()方法可以进行参数的传递,
      //次参数作为yield表达式的返回值 next优先 yield次之
      a.next("aaa"); //one-- aaa
      a.next("bbb"); //one-- bbb
      a.next("ccc"); //one-- ccc
    </script>
  </body>
</html>

 

 Generator模拟用户下单

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 模拟用户下单的过程
      // 获取用户数据(登录携带用户数据) -> 浏览商品(商品信息) -> 用户下单(用户id) -> 订单
      //用户ID -> 订单数据 订单id -> 订单里的商品数据

      //获取用户数据
      function getUser() {
        // 延时函数
        setTimeout(() => {
          let userId = "我是用户id";
          //调用函数
          a.next(userId);
        }, 2000);
      }
      //通过用户id获取订单数据
      function getOrder(userId) {
        setTimeout(() => {
          let orderId = "我是订单id";
          a.next(orderId);
        });
      }
      //根据订单id获取商品数据
      function getGoods(orderId) {
        setTimeout(() => {
          let goodsData = "商品数据";
          a.next(goodsData);
        });
      }

      //需求:原本按照逻辑分析,以上三种方法是嵌套结构,拿到上一个数据才能完成下一个请求
      //es6解决:使用generator函数将以上三种数据放入状态机管理  手动去控制哪个方法执行
      //1.创建generator函数
      function* gen() {
        console.log("订单开始处理");
        //处理用户信息的,拿用户id
        let userId = yield getUser();
        console.log(userId);

        //根据用户id获取订单数据的方法
        let orderId = yield getOrder(userId);
        console.log(orderId);

        //根据订单id获取产品数据的方法
        let goodsData = yield getGoods(orderId);
        console.log(goodsData);
      }

      //2.执行
      let a = gen();
      //想要手动控制执行哪个方法,得用next()
      a.next();
    </script>
  </body>
</html>

 

async的使用:

 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <!-- 
        //获取轮播图 get
        http://47.100.84.201:8888/carousel/findAll
        //获取所有用户 get
        http://47.100.84.201:8888/baseUser/findAll
     -->
  </head>
  <body>
    <script>
      //声明函数 这个函数里 要先过去到用户的数据 再获取到轮播图的数据
      //async和await是generator的语法糖

      //没有await,只是async修的是方法,表示都是异步加载,输出谁先谁后
      //不一定是先1后2,因为都是异步加载,先后就是看谁先返回谁
      //所以想要限制一定要从1 开始,就需要加上await,1如果没有拿到数据/不成功,2一定拿不到
      async function getData() {
        //获取用户数据
        let userData = await $.get(
          "http://47.100.84.201:8888/carousel/findAll"
        );
        console.log("1", userData);
        //获取轮播图数据
        let swiperData = await $.get(
          "http://47.100.84.201:8888/baseUser/findAll"
        );
        console.log("2", swiperData);
      }
      getData();
    </script>
  </body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值