当前位置: 首页 / 技术干货 / 正文
前端技术干货-Promise的相关介绍

2023-03-28

promise  ■ 失败

前端技术干货-Promise的相关介绍

  ●Promise

  是一种异步代码的封装方案

  因为换了一种封装方案, 不需要安装回调函数的方式去调用, 需要按照 promise 的形式去调用

  注意 promise 不是解决 异步问题的, 而是解决回调地狱问题的

  ●认识 Promise

  ○promise 的三种状态

  ■持续: pending

  ■成功: fulfilled

  ■失败: rejected

  ○promise 的两种转换

  ■从持续转为成功

  ■从持续转为失败

  ○promise 的基础语法

  ■ES6 内置构造函数

  ○promise 的语法

  ■const p = new Promise(function () {})

  ○promise 对象可以触发的两个方法

  ■p.then(函数); 成功时执行

  ■p.catch(函数); 失败时执行

  ●promise 封装一个异步函数

  const p = new Promise(function (resolve, reject) {

  // resolve: 是一个形参, 名字自定义, 值是一个函数, 当你调用的时候, 会把当前 promise 的状态转换为 成功

  // reject: 是一个形参, 名字自定义, 值是一个函数, 当你调用的时候, 会把当前 promise 的状态转换为 失败

  // resolve 和 reject 调用时可以传递一个参数, 这个参数会被传递给对应的 then catch

  const timer = Math.ceil(Math.random() * 3000) + 2000;

  setTimeout(() => {

  if (timer > 3500) {

  console.log("买水失败, 耗时 ", timer);

  reject("奖励一个bug");

  } else {

  console.log("买水成功, 耗时: ", timer);

  resolve("送你十个bug");

  }

  }, timer);

  });

  p.then(function (address) {

  console.log("班长买水成功咯~~~", address);

  });

  p.catch(function (address) {

  console.log("班长买水失败咯~~~", address);

  });

  封装 promise 为函数

  function fn() {

  const p = new Promise(function (resolve, reject) {

  const timer = Math.ceil(Math.random() * 3000) + 2000;

  setTimeout(() => {

  if (timer > 3500) {

  reject("班长买水失败");

  } else {

  resolve("班长买水成功");

  }

  }, timer);

  });

  return p;

  }

  // 将来在使用的时候 res 得到的是 promise 的实例对象 p

  const res = fn();

  res.then(function (type) {

  // 这个函数执行代码 promise 状态为成功状态!!!

  console.log("因为", type, "谢谢班长, 我准备了20个bug, 回馈给你");

  });

  res.catch(function (type) {

  // 这个函数执行代码

  console.log("因为", type, "谢谢班长, 我准备了800个bug, 开心死你");

  });

  ●promise 的链式调用

  fn()

  .then(function (type) {

  // 这个函数执行代码 promise 状态为成功状态!!!

  console.log("因为", type, "谢谢班长, 我准备了20个bug, 回馈给你");

  })

  .catch(function (type) {

  // 这个函数执行代码

  console.log("因为", type, "谢谢班长, 我准备了800个bug, 开心死你");

  });

  ●promise 的调用方式补充

  ○如果你在第一个 then 里面返回(return) 一个新的 promise 对象的时候

  ○可以在第一个 then 后面, 继续第二个 then

  fn()

  .then(function (type) {

  console.log(

  "第一次: 因为",

  type,

  "谢谢班长, 我准备了20个bug, 回馈给你"

  );

  return fn();

  })

  .then(function (type) {

  console.log(

  "第二次: 因为",

  type,

  "谢谢班长, 我准备了20个bug, 回馈给你"

  );

  return fn();

  })

  .then(function (type) {

  console.log(

  "第三次: 因为",

  type,

  "谢谢班长, 我准备了20个bug, 回馈给你"

  );

  return fn();

  })

  .catch(function (type) {

  console.log("因为", type, "谢谢班长, 我准备了800个bug, 开心死你");

  });

  ●promise 的其他方法

  ●Promise 实例的 finally 方法

  ○不管promise是成功还是失败, 只要 promise 执行结束, 我都会执行

  fn()

  .then(function (res) {

  console.log("成功");

  })

  .catch(function (res) {

  console.log("失败");

  })

  .finally(function () {

  console.log(

  "不管promise是成功还是失败, 只要 promise 执行结束, 我都会执行"

  );

  });

  Promise 本身还有一些方法

  ○all:

  ■作用: 可以同时触发多个 promise 行为

  ●只有所有的 promise 都成功的时候, all 才算成功

  ●只要任何一个 promise 失败的时候, all 就算失败了

  ■语法: Promise.all([多个 promise])

  ○race:

  ■作用: 可以同时触发多个 promise 行为

  ●按照速度计算, 当第一个结束的时候就结束了, 成功或失败取决于第一个执行结束的 promise

  ■语法: Promise.race([多个 promise])

  ○allSettled

  ■作用: 可以同时触发多个 Promise 行为

  ●不管多个成功还是失败都会触发

  ●会在结果内以数组的形式给你返回 每一个 promise 行为的成功还是失败

  ■语法: Promise.allSettled([多个 promise])

  ○resolve

  ■作用: 强制返回一个成功状态的 promise 对象

  ○reject

  ■作用: 强制返回一个失败状态的 promise 对象

  // 1. all

  Promise.all([fn(), fn(), fn()])

  .then(function () {

  console.log("所有的 参数 都返回 成功状态");

  })

  .catch(function () {

  console.log("这些参数中, 有一个 为 失败状态");

  });

  // 2. race

  Promise.race([fn(), fn(), fn()])

  .then(function () {

  console.log("速度最快的那个执行完毕, 并且是成功状态时 执行");

  })

  .catch(function () {

  console.log("速度最快的那个执行完毕, 并且是失败状态时 执行");

  });

  // 3. allSettled

  Promise.allSettled([fn(), fn(), fn()])

  .then(function (res) {

  console.log(res);

  })

  .catch(function (res) {

  console.log(res);

  });

  // 4. resolve

  Promise.resolve()

  .then(function (res) {

  console.log("成功");

  })

  .catch(function (res) {

  console.log("失败");

  });

  // 5. reject

  Promise.reject()

  .then(function (res) {

  console.log("成功");

  })

  .catch(function (res) {

  console.log("失败");

  });

好程序员公众号

  • · 剖析行业发展趋势
  • · 汇聚企业项目源码

好程序员开班动态

More+
  • HTML5大前端 <高端班>

    开班时间:2021-04-12(深圳)

    开班盛况

    开班时间:2021-05-17(北京)

    开班盛况
  • 大数据+人工智能 <高端班>

    开班时间:2021-03-22(杭州)

    开班盛况

    开班时间:2021-04-26(北京)

    开班盛况
  • JavaEE分布式开发 <高端班>

    开班时间:2021-05-10(北京)

    开班盛况

    开班时间:2021-02-22(北京)

    开班盛况
  • Python人工智能+数据分析 <高端班>

    开班时间:2021-07-12(北京)

    预约报名

    开班时间:2020-09-21(上海)

    开班盛况
  • 云计算开发 <高端班>

    开班时间:2021-07-12(北京)

    预约报名

    开班时间:2019-07-22(北京)

    开班盛况
IT培训IT培训
在线咨询
IT培训IT培训
试听
IT培训IT培训
入学教程
IT培训IT培训
立即报名
IT培训

Copyright 2011-2023 北京千锋互联科技有限公司 .All Right 京ICP备12003911号-5 京公网安备 11010802035720号