zoukankan      html  css  js  c++  java
  • Promise实例的finally方法

    Promise.prototype.finally()

    finally( )方法用于指定不管Promise对象最后状态如何,都会执行的操作,不管promise最后的状态,在执行完 then或catch指定的回调函数以后,都会执行finally方法指定的回调函数。

    promise
    .then(result => {···})
    .catch(error => {···})
    .finally(() => {···});

       finally方法的回调函数不接受任何参数,这意味着没有办法知道,前面的 Promise 状态到底是fulfilled还是rejected。这表明,finally方法里面的操作,应该是与状态无关的,不依赖于 Promise 的执行结果。

     finally方法本质上是then方法的特例,

    promise
    .finally(() => {
      // 语句
    });
    
    // 等同于
    promise
    .then(
      result => {
        // 语句
        return result;
      },
      error => {
        // 语句
        throw error;
      }
    );

         上面代码中,如果不使用finally方法,同样的语句需要为成功和失败两种情况各写一次。有了finally方法,则只需要写一次。

  • 相关阅读:
    Android SDK
    1055
    清除浮动的三种方式
    解决块状元素垂直外边距的塌陷问题
    drf 验证接口权限
    Linux常用指令
    Linux安装python3,virtualenv和virtualenvwrapper
    Linux基本命令2
    Linux之文档与目录结构
    Linux基本命令
  • 原文地址:https://www.cnblogs.com/zhishiyv/p/14303255.html
Copyright © 2011-2022 走看看