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);
}
},
回复
1个回答

test
2024-07-02
在异步函数内使用 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();
})
},
}
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容