在Vue单页面应用中使用Promise链式调用


eg:

  this.commonLoginFun().then((res) => {
      if (res.errNo === 0) {
        const { isLogin } = res.data;
        if (isLogin) {
          this.isLogin = true;
          this.userPhone = res.data.phone;
        } else {
          this.isLogin = false;
        }
      }
    });

在commonLoginFun中:

methods:{

  commonLoginFun(){

    return xxx.axios.xxxx;  //2个return,把异步函数return,把异步函数的返回值return。 

  }

}

总结:

1. 使用then方法之后会返回一个promise对象,可以继续使用then方法调用,再次调用所获得的参数是上个then方法return的内容。

2. 将一个promise对象当做参数传递给promise.resolve()会被直接返回。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM