【前端学习】—Promise基本知识(十六)

【前端学习】---Promise基本知识(十六)

一、class和function的区别

  • 相同点:都可以用作构造函数
  • 不同点:class不可以使用call apply bind的方式来改变它的执行上下文

二、Promise

定义:Promise是异步编程的一种解决方案,比传统的解决方案------回调函数和事件更合理和更强大。它是由社区最早提出和实现,ES6将其写进了语言标准,统一了用法,原生提供了Promise对象

三、代码演示

bash 复制代码
<script>

        //3个状态  pending fulfilled rejected
      new Promise(function (resolve, reject) {
        let fullName = "cai";
        if (fullName === "cai") {
          resolve(1);
        } else {
          reject(2);
        }
      }).then(
        function (value) {
          console.log(`resolve-value`, value);//resolve-value 1
        },
        function (value) {
          console.log(`reject-value`, value);
        }
      );
    </script>
bash 复制代码
<script>
      //Promise.prototype.then
      //可以支持链式调用
      //then接收的两个函数都是参数
      //第一个函数是Promise  状态变成fulfilled时候回调函数
      //第二个函数也是Promise 状态变成rejected的时候调用
      //返回值也是Promise(新的)

      new Promise(function (resolve, reject) {
        let fullName = "lily";
        if (fullName === "lily") {
          resolve(1);
        } else {
          reject(2);
        }
      })
        .then((value) => {
          console.log(`fulfilled value`, value);
          return value + 1;
        })
        .then(
          (fulfilledValue) => {
            console.log(`fulfilled value`, fulfilledValue);
          },
          (rejectedValue) => {
            console.log(`rejectedValue`, rejectedValue);
          }
        );
    </script>
bash 复制代码
//Promise.prototype.catch()捕获promise错误
      new Promise(function (resolve, reject) {
        let name = "ll";
        if (name === "llu") {
          resolve(1);
        } else {
          reject("promise error");
        }
      })
        .then((value) => {
          console.log(value);
        })
        .catch((error) => {
          console.log(error); //promise error
        });
bash 复制代码
 //Promise.prototype.finally()用于指定不管promise对象最后状态如何 都会执行的操作


      new Promise(function (resolve, reject) {
        console.log(`loading start......`)
        let name = "ll";
        if (name === "llu") {
          resolve(1);
        } else {
          reject("promise error");
        }
      })
        .then((value) => {
          console.log(value);
        })
        .catch((error) => {
          console.log(error); //promise error
        }).finally(()=>{
            console.log(`loading end......`)
        });
bash 复制代码
 // Promise.all
    //用于将多个Promise实例包装成一个新的Promise实例
      const promiseArray=[1,2,3,4,5].map((item)=>{
        return new Promise((resolve)=>{
            resolve(item);
        })
      })

      Promise.all(promiseArray).then(res=>{
        console.log(`res`,res);
      })
bash 复制代码
  // Promise.race方法是将多个promise实例,包装成一个新的Promise实例
      //Promise.race 继发(挨个发送)
      //Promise.all并发(多个一起发送)
       
        const promiseArray=[1,2,3,4,5].map((item)=>{
        return new Promise((resolve)=>{
            resolve(item);
        })
      })
      Promise.race(promiseArray).then(res=>{
        console.log(`res`,res);//res 1
      })
bash 复制代码
// Promise.resolve将现有对象 转换成Promise对象
Promise.resolve(1).then(res=>{
    console.log(res)//1
})
bash 复制代码
// Promise.reject 返回一个Promise实例 ------>rejected
Promise.reject('promise error').then(null,rejected=>{
    console.log(`rejected`,rejected)
})

四、ES6扩展运算符...实现原理

相关推荐
yinuo24 分钟前
轻松接入大语言模型API -04
前端
袋鼠云数栈UED团队1 小时前
基于 Lexical 实现变量输入编辑器
前端·javascript·架构
cipher1 小时前
ERC-4626 通胀攻击:DeFi 金库的"捐款陷阱"
前端·后端·安全
UrbanJazzerati1 小时前
非常友好的Vue 3 生命周期详解
前端·面试
AAA阿giao1 小时前
从零构建一个现代登录页:深入解析 Tailwind CSS + Vite + Lucide React 的完整技术栈
前端·css·react.js
兆子龙2 小时前
像 React Hook 一样「自动触发」:用 Git Hook 拦住忘删的测试代码与其它翻车现场
前端·架构
兆子龙3 小时前
用 Auto.js 实现挂机脚本:从找图点击到循环自动化
前端·架构
SuperEugene3 小时前
表单最佳实践:从 v-model 到自定义表单组件(含校验)
前端·javascript·vue.js
昨晚我输给了一辆AE863 小时前
为什么现在不推荐使用 React.FC 了?
前端·react.js·typescript
不会敲代码13 小时前
深入浅出 React 闭包陷阱:从现象到原理
前端·react.js