首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >JS / Vue JS - api调用的冗余if语句

JS / Vue JS - api调用的冗余if语句
EN

Stack Overflow用户
提问于 2021-08-10 03:59:14
回答 1查看 25关注 0票数 1

我有以下函数,其中if语句感觉有点多余,因为我重复了两次对sendProfileData的调用。有没有人能教我怎样才能把这个弄干净?

代码语言:javascript
运行
AI代码解释
复制
 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);
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-10 04:03:27

我会把它移到它自己的函数中

代码语言:javascript
运行
AI代码解释
复制
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});
      }
  })
}

然后从两个点调用它

代码语言:javascript
运行
AI代码解释
复制
if (!formData.fileUploader) {
  uploadProfile(initialImage)
...
uploadProfilePicture(formData.fileUploader).then(response => {
  if (response) {
    uploadProfile(response.filename)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68726421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档