我有以下函数,其中if语句感觉有点多余,因为我重复了两次对sendProfileData的调用。有没有人能教我怎样才能把这个弄干净?
function editDetails(formData) {
const initialImage = teacherProfile.value.profilePictureUrl.substring(teacherProfile.value.profilePictureUrl.indexOf("/teacher"))
if (!formData.fileUploader) {
sendProfileData(formData, initialImage).then(response => {
if (response) {
// After the successful API call, go back to the teachers profile
router.push({name: ROUTE_NAMES_TEACHER_PROFILE.TEACHER_PROFILE});
}
})
} else {
uploadProfilePicture(formData.fileUploader).then(response => {
if (response) {
sendProfileData(formData, response.filename).then(response => {
if (response) {
// After the successful API call, go back to the teachers profile
router.push({name: ROUTE_NAMES_TEACHER_PROFILE.TEACHER_PROFILE});
}
})
}
})
}
}
// Dispatch action
function sendProfileData(data, imageUrl) {
return store.dispatch(SET_PROFILE_DATA, {data, imageUrl});
}
function uploadProfilePicture(file) {
return store.dispatch(UPLOAD_PROFILE_IMAGE, file);
}
发布于 2021-08-10 04:03:27
我会把它移到它自己的函数中
function uploadProfile(file) {
sendProfileData(formData, file).then(response => {
if (response) {
// After the successful API call, go back to the teachers profile
router.push({name: ROUTE_NAMES_TEACHER_PROFILE.TEACHER_PROFILE});
}
})
}
然后从两个点调用它
if (!formData.fileUploader) {
uploadProfile(initialImage)
...
uploadProfilePicture(formData.fileUploader).then(response => {
if (response) {
uploadProfile(response.filename)
https://stackoverflow.com/questions/68726421
复制相似问题