1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        Promise.prototype.finally 的作用,如何自己實(shí)現(xiàn) Promise.prototype.finally

        共 2234字,需瀏覽 5分鐘

         ·

        2021-05-22 23:45


        點(diǎn)擊上方 三分鐘學(xué)前端,關(guān)注公眾號

        回復(fù)交流,加入前端編程面試算法每日一題群


        面試官也在看的前端面試資料

        Promise.prototype.finally()  的作用

        Promise.prototype.finally() 是 ES2018 新增的特性,它回一個 Promise ,在 promise 結(jié)束時,無論 Promise 運(yùn)行成功還是失敗,都會運(yùn)行 finally ,類似于我們常用的  try {...} catch {...} finally {...}

        Promise.prototype.finally()  避免了同樣的語句需要在 then()catch() 中各寫一次的情況

        new Promise((resolve, reject) => {
          setTimeout(() => resolve("result"), 2000)
        })
          .then(result => console.log(result))
          .finally(() => console.log("Promise end"))

        // result
        // Promise end

        reject :

        new Promise((resolve, reject) => {
          throw new Error("error")
        })
          .catch(err => console.log(err))
          .finally(() => console.log("Promise end"))

        // Error: error
        // Promise end

        注意:

        • finally 沒有參數(shù)
        • finally 會將結(jié)果和 error 傳遞
        new Promise((resolve, reject) => {
          setTimeout(() => resolve("result"), 2000)
        })
          .finally(() => console.log("Promise ready"))
          .then(result => console.log(result))

        // Promise ready
        // result

        手寫一個 Promise.prototype.finally()

        不管 Promise 對象最后狀態(tài)如何,都會執(zhí)行的操作

        MyPromise.prototype.finally = function (cb{
          return this.then(function (value{
            return MyPromise.resolve(cb()).then(function ({
              return value
            })
          }, function (err{
            return MyPromise.resolve(cb()).then(function ({
              throw err
            })
          })
        }


        來自:https://github.com/sisterAn/blog

        最后

        歡迎關(guān)注「三分鐘學(xué)前端」,回復(fù)「交流」自動加入前端三分鐘進(jìn)階群,每日一道編程算法面試題(含解答),助力你成為更優(yōu)秀的前端開發(fā)!
        》》面試官也在看的前端面試資料《《
        “在看和轉(zhuǎn)發(fā)”就是最大的支持


        瀏覽 36
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            亚洲草逼图| 性伦h2泰山电影 加勒比毛片 | 我想看操逼大片 | 粉嫩壁肉被粗大慢慢撑开视频 | 奇米无码视频 | 午夜久 | 萍萍的性荡生活第三部 | 白石真琴在线 | 国产女性无套 免费网站 | 黄色三级日韩 |