vue2 return失效?

async pdfConfirm(docId) {
  // debugger;
  let data = { docId: docId, isPost: 1 };
  // changeStatus 状态为4调确认接口
  if (this.newFileParam.changeStatus == 5) {
    // 新文件确认
    await newFileConfirm({ modifyNo: this.newFileParam.modifyNo })
      .then(res => {
        if (res.code == '999999') {
          this.dialogVisible = false;
          this.reflashGoods();
          return;
        }
      })
      .catch(error => {
        console.log(error.message);
        this.dialogVisible = false;
        this.reflashGoods();
        return;
      });
  }
  // 旧文件确认
  modifityPdfNote(data)
    .then(res => {
      // console.log(res.data);
      console.log('1111');
      this.dialogVisible = false;
      this.reflashGoods();
    })
    .catch(() => {
      this.dialogVisible = false;
      this.reflashGoods();
    });
},

为什么return没用

我改成这样是可以的
// 旧文件确认

oldFilesConfirm(data) {
  // 旧文件确认
  modifityPdfNote(data)
    .then(res => {
      // console.log(res.data);
      console.log('1111');
      this.dialogVisible = false;
      this.reflashGoods();
    })
    .catch(() => {
      this.dialogVisible = false;
      this.reflashGoods();
    });
},
// pdf 确认窗口
async pdfConfirm(docId) {
  // debugger;
  let data = { docId: docId, isPost: 1 };
  // changeStatus 状态为4调确认接口
  if (this.newFileParam.changeStatus == 5) {
    // 新文件确认
    newFileConfirm({ modifyNo: this.newFileParam.modifyNo })
      .then(res => {
        if (res.code == '000000') {
          this.oldFilesConfirm(data);
        } else {
          this.dialogVisible = false;
          this.reflashGoods();
        }
      })
      .catch(error => {
        console.log(error.message);
        this.dialogVisible = false;
        this.reflashGoods();
      });
  } else {
    this.oldFilesConfirm(data);
  }
},
阅读 2.4k
2 个回答

在异步函数内使用 return 当然没有效果啊……
看了一下你的两个 return 的位置,所以直接使用 if……else 不就好了吗。

async pdfConfirm(docId) {
  ...
  if (this.newFileParam.changeStatus == 5) {
    // 新文件确认
    await newFileConfirm({ modifyNo: this.newFileParam.modifyNo })
      .then(res => {
        if (res.code == '999999') {
          this.dialogVisible = false;
          this.reflashGoods();
-          return;
        }
      })
      .catch(error => {
        console.log(error.message);
        this.dialogVisible = false;
        this.reflashGoods();
-        return;
      });
-  }
+  } else {  
    // 旧文件确认
    modifityPdfNote(data)
    .then(res => {
    ...
+  }
}

更新:

既然你本来调用就是链式的,所以直接把新/旧文件确认拿到外部作为单独的函数就好了,也就不需要使用 async/await 了,因为你其实也不需要去等待。
大概改了一下业务逻辑,你自己还可以优化一下:

methods:{
  pdfConfirm(docId) {
    let data = { docId: docId, isPost: 1 };
    // changeStatus 状态为4调确认接口
    if (this.newFileParam.changeStatus == 5) {
      this.newFileConfirm({ modifyNo: this.newFileParam.modifyNo }).then(() => this.modifityPdfNote(data))
    } else {
      this.modifityPdfNote(data)
    }
  },
  // 新文件确认
  newFileConfirm(data){
    return new Promise((resolve, reject) => {
      newFileConfirm(data)
        .then(res => {
          if (res.code == '999999') {
            resolve()
          } else {
            reject()
          }
        })
        .catch(error => {
          reject()
        })
        .finally(() => {
          this.dialogVisible = false;
          this.reflashGoods();
        })
    })
  },
  // 旧文件确认
  modifityPdfNote(data){
    modifityPdfNote(data)
      .then(res => {
        // console.log(res.data);
        console.log('1111');
      })
      .catch(() => {
      })
      .finally(() => {
        this.dialogVisible = false;
        this.reflashGoods();
      })
  },
}

return 是跳出当前函数,上一级函数会继续执行。

要连续退出,你需要返回一个特殊的值,在外层函数里判断,然后继续 return

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进