项目中有一个地方需要获取到接口返回值之后根据返回值确定之后执行的步骤,使用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.
    }
  })
}

 

Logo

前往低代码交流专区

更多推荐