Skip to content

实现一个 Promise.prototype.finally

js
Promise.prototype.finally = function (callback) {
  this.then(
    (value) => {
      return Promise.resolve(callback()).then(() => {
        return value
      })
    },
    (error) => {
      return Promise.resolve(callback()).then(() => {
        throw error
      })
    },
  )
}

如有转载或 CV 的请标注本站原文地址