Promise(简介、基本使用、API、手写实现 Promise、async与await)(五)

简介: Promise(简介、基本使用、API、手写实现 Promise、async与await)(五)

5.4 Promise.catch()实现

5.4.1 catch()实现

// 定义 Promise 的 catch 方法
Promise.prototype.catch = function (onRejected) {
  // catch 方法中执行的是失败的回调函数
  // 与 then 差别为只有失败的回调函数,可以直接使用then
  return this.then( undefined, onRejected )
}
let p1 = new Promise((resolve, reject) => {
        setTimeout(() => {
          reject('Error')
        }, 1000)
      })
      const res1 = p1.catch(reason=>{
        console.log(reason)
      })
      console.log(res1)

5.4.2 异常穿透实现

在then的链式调用的过程中不需要对失败的结果进行处理,主要在最后加一个catch方法处理失败的结果即可。

未实现异常穿透测试:

let p1 = new Promise((resolve, reject) => {
        setTimeout(() => {
          reject('Error')
        }, 1000)
      })
      p1.then((v) => {
        console.log(111)
      })
        .then((v) => {
          console.log(222)
        })
        .catch((r) => {
          console.log(r)
        })

原因:

实现异常穿透:

// 定义 Promise 的 then 方法
Promise.prototype.then = function (onResolved, onRejected) {
  const self = this // 保存调用then的Promise对象
  // 判断失败的回调函数
  // 由于报错造成的原因是失败的回调函数为undefined
  // 所以当失败的回调函数为未定义时候,补充一个失败回调
  // 让其报错
  if (typeof onRejected !== 'function' ) {
    onRejected = reason => {
      throw reason
    }
  }
  ......
}

测试:

let p1 = new Promise((resolve, reject) => {
        setTimeout(() => {
          // reject('Error')
          resolve('OK')
        }, 1000)
      })
      p1.then((v) => {
        console.log(111)
        throw 'ERROR'
      })
        .then((v) => {
          console.log(222)
        })
        .catch((r) => {
          console.log(r)
        })

5.4.3 值传递实现

官方的then()在不指定成功的回调函数也可以继续向下执行

let p1 = new Promise((resolve, reject) => {
        setTimeout(() => {
          // reject('Error')
          resolve('OK')
        }, 1000)
      })
      p1.then()
        .then((v) => {
          console.log(222)
        })
        .then((v) => {
          console.log(333)
        })
        .catch((r) => {
          console.log(r)
        })

实现方法,与异常穿透类似,在没有成功的回调函数的时候一样指定一个成功的回调函数

// 定义 Promise 的 then 方法
Promise.prototype.then = function (onResolved, onRejected) {
  const self = this // 保存调用then的Promise对象
  ......
  // 没有传递成功的回调函数
  // 给一个默认的成功回调函数
  if (typeof onResolved !== 'function') {
    onResolved = value => {
      return value //将resolve中传递的值,即Promise的结果向后传递
    }
  }
  ......
}

测试:

let p1 = new Promise((resolve, reject) => {
        setTimeout(() => {
          // reject('Error')
          resolve('OK')
        }, 1000)
      })
      p1.then()
        .then((v) => {
          console.log(222)
        })
        .then((v) => {
          console.log(333)
        })
        .catch((r) => {
          console.log(r)
        })

5.5 resolve() 实现

// 传递的为Promise对象,则返回的Promise的状态由传入的决定
// 传入的为其他值,则返回的Promise对象为成功
Promise.resolve(value) {
  return new Promise((resolve, reject) => {
    if (value instanceof Promise) {
      // 为Promise对象,肯定可以调用then
      value.then(v => {
        resolve(v)
      }, r => {
        reject(r)
      })
    } else {
      resolve(value)
    }
  })
}

测试:

const p1 = Promise.resolve('OK')
      const p2 = Promise.resolve(
        new Promise((resolve, reject) => {
          resolve('OK~~~')
        })
      )
      const p3 = Promise.resolve(
        new Promise((resolve, reject) => {
          reject('err')
        })
      )
      console.log(p1)
      console.log(p2)
      console.log(p3)

5.6 reject() 实现

无论传递什么值都返回一个失败的Promise对象

Promise.reject = function(value) {
  return new Promise((resolve, reject) => {
    reject(value)
  })
}

测试:

const p1 = Promise.reject('OK')
      const p2 = Promise.reject(
        new Promise((resolve, reject) => {
          resolve('OK~~~')
        })
      )
      const p3 = Promise.reject(
        new Promise((resolve, reject) => {
          reject('err')
        })
      )
      console.log(p1)
      console.log(p2)
      console.log(p3)


相关文章
|
6天前
|
前端开发 JavaScript
Promise、async和await
Promise、async和await
11 0
|
1月前
|
前端开发 小程序 API
【微信小程序】-- 使用 npm 包 - API Promise化(四十二)
【微信小程序】-- 使用 npm 包 - API Promise化(四十二)
|
1月前
|
数据采集 前端开发 JavaScript
如何在爬虫过程中正确使用Promise对象和async/await?
如何在爬虫过程中正确使用Promise对象和async/await?
20 2
|
1月前
|
前端开发 JavaScript 开发者
JavaScript 中的异步编程:Promise 和 Async/Await
在现代的 JavaScript 开发中,异步编程是至关重要的。本文将介绍 JavaScript 中的异步编程概念,重点讨论 Promise 和 Async/Await 这两种常见的处理异步操作的方法。通过本文的阐述,读者将能够更好地理解和应用这些技术,提高自己在 JavaScript 开发中处理异步任务的能力。
|
4天前
|
前端开发 JavaScript Java
Promise, async, await实现异步编程,代码详解
Promise, async, await实现异步编程,代码详解
16 1
|
25天前
|
前端开发
promise和async的区别是什么?
promise和async的区别是什么?
10 1
|
1月前
|
存储 前端开发 安全
【C++并发编程】std::future、std::async、std::packaged_task与std::promise的深度探索(三)
【C++并发编程】std::future、std::async、std::packaged_task与std::promise的深度探索
27 0
|
1月前
|
存储 设计模式 前端开发
【C++并发编程】std::future、std::async、std::packaged_task与std::promise的深度探索(二)
【C++并发编程】std::future、std::async、std::packaged_task与std::promise的深度探索
30 0
|
1月前
|
并行计算 前端开发 安全
【C++并发编程】std::future、std::async、std::packaged_task与std::promise的深度探索(一)
【C++并发编程】std::future、std::async、std::packaged_task与std::promise的深度探索
64 0
|
1月前
|
前端开发 JavaScript
如何处理 JavaScript 中的异步操作和 Promise?
如何处理 JavaScript 中的异步操作和 Promise?
15 1