promise里面then的用法

112 阅读1分钟
<script>
  // 初始化一个pormise
  //   里面有箭头函数,函数里面有2个参数,这两个会定义promise的状态
  const p = new Promise((resolve, reject) => {
    resolve(); /* 传入了solve执行,promise的状态现在是成功态 */
  });
  //   然后开始写then
  //   then里面也是两个箭头函数
  p.then(
    // 当成功态的时候执行这个
    () => {
      console.log("success");
    },
    // 当promise是失败态的时候执行这个
    () => {
      console.log("fail");
    }
  );
</script>