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

时间:2023-03-10 08:22:21
在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()会被直接返回。