当前位置: 代码迷 >> 综合 >> vue方法同步(顺序)执行:async/await使用
  详细解决方案

vue方法同步(顺序)执行:async/await使用

热度:0   发布时间:2023-12-23 15:00:01.0

项目中有一个地方需要获取到接口返回值之后根据返回值确定之后执行的步骤,使用async搭配await实现,await函数不能单独使用。方法如下:

async methodName(params){let isSuccess = false;await this.$http({url: URL,method: "get",params: this.$http.adornParams({params:params})}).then(({ data }) => {if (data && data.code === 0) {if(data.exist == 0){isSuccess = true}}}).catch(err => {console.log(err);this.$message({type: "error",message: "系统异常"});});return isSuccess
}

async函数返回的是一个Promise对象,可以使用then函数添加回调函数

methodaa() {this.methodName(this.params).then(function (result) { if (result) {// do sth.}else {// do other sth.}})
}

 

  相关解决方案