老师帮我优化一下这段代码: //发起培训提交 const startUpdate = async () =>{ let sumbit = JSON.parse(JSON.stringify(form.data)); formData.value.submit(async (res)=> { sumbit.trainStatus = 1; formTrainDate.value.submit(async (res)=>{ sumbit.trainStartTime = trainDate.data.trainDateData[0]; sumbit.trainEndTime = trainDate.data.trainDateData[1]; if (sumbit.empState != 0 ){ //查他选择的岗位是否和他已经有的岗位重合,如果重合,重合的岗位的等级不得选择比他自己已有等级更低 let res = await getUserList({id: form.data.empId}); if (res.status && res.data?.dataSource[0]['skill']?.length>0){ let post = res.data.dataSource[0]['skill'].find((item)=> item.postId === form.data.postId); if(post) { let index = certificationLevel.findIndex((item)=> item.name === post.certificationLevel); let choseIndex = certificationLevel.findIndex((item)=> item.name === form.data.certificationLevel); if (index>=choseIndex){ promptContent.value = '所选岗位等级不得低于员工原有此岗位等级:'+post.certificationLevel; promptDialog.value.open(); return; } } } } let resMsg = await personnelTrainUpdate(sumbit); if (afterSubmit(resMsg)){ uni.switchTab({ url:"/pages/tabPages/allPages/index", success:()=>{ uni.navigateTo({ url:"/pages/qualityPages/personnelTrain/index?index=2", }); } }) } }) }) }
时间: 2024-02-24 14:59:55 浏览: 196
destr:JSON.parse的更快,安全和方便的替代方法
好的,我来帮您优化一下这段代码。
首先,我建议您使用ES6中的箭头函数来定义函数,以保持代码的简洁性和可读性。
其次,您可以使用`await`关键字来等待异步操作返回结果,以保证代码的执行顺序和可靠性。
最后,您可以使用`try...catch`语句来捕捉可能出现的异常,以避免程序崩溃。
参考下面的代码,希望对您有所帮助:
```
const startUpdate = async () => {
try {
let submit = { ...form.data };
await formData.value.submit();
submit.trainStatus = 1;
await formTrainDate.value.submit();
submit.trainStartTime = trainDate.data.trainDateData[0];
submit.trainEndTime = trainDate.data.trainDateData[1];
if (submit.empState !== 0) {
let res = await getUserList({ id: form.data.empId });
if (res.status && res.data?.dataSource[0]['skill']?.length > 0) {
let post = res.data.dataSource[0]['skill'].find((item) => item.postId === form.data.postId);
if (post) {
let index = certificationLevel.findIndex((item) => item.name === post.certificationLevel);
let choseIndex = certificationLevel.findIndex((item) => item.name === form.data.certificationLevel);
if (index >= choseIndex) {
promptContent.value = '所选岗位等级不得低于员工原有此岗位等级:' + post.certificationLevel;
promptDialog.value.open();
return;
}
}
}
}
let resMsg = await personnelTrainUpdate(submit);
if (afterSubmit(resMsg)) {
uni.switchTab({
url: "/pages/tabPages/allPages/index",
success: () => {
uni.navigateTo({
url: "/pages/qualityPages/personnelTrain/index?index=2",
});
},
});
}
} catch (e) {
console.error(e);
}
};
```
阅读全文