doureng6738 2019-06-06 21:48
浏览 230
已采纳

在组件内的vuex动作上承诺then()

generally I like to keep most of the logic behind vuex actions inside my modules, this way I keep components clean and most of the logic gathered in modules (this seems optimal form me), the issue is sometimes I'll need to do some action inside component data after the action (generally invlving an axios promise) finishes(like for example, clearing a form after a successfull ajax call), I thought I solved this by adding a then closure to my vuex action call and returning axios promise in my module but I noticed that the then closure will always resolve inmediatelly instead of only when everything goes right, 200 OK.

Here is my component :

stripeSourceHandler: function(sourceId)
    {
        if(this.customerSources.length == 0)
        {
            console.log('createSourceAndCustomer');
            this.createSourceAndCustomer({ id: sourceId, paymentCity:this.paymentCity, paymentAddress:this.paymentAddress })
            .then(() => {
                this.clearForm();
            });
        }
}

My vuex module action:

createSourceAndCustomer({ commit }, sourceData)
    {
        commit('Loader/SET_LOADER', { status:1, message: 'Añadiendo forma de pago...' }, { root: true });
        return axios.post('/stripe/create-source-and-customer', sourceData)
        .then((response) => {
            commit('Loader/SET_LOADER', { status:2, message: response.data.message }, { root: true });
            commit('CREATE_SOURCE_AND_CUSTOMER', response.data.customer);
        }, 
        (error) => {
            commit('Loader/SET_LOADER', { status:3, errors: error, message: 'Oops, algo salio mal..' }, { root: true });
        });
    },

So to summarize, I want the clearForm method to happen only if the axios call was successful instead of always firing.

  • 写回答

1条回答 默认 最新

  • dpsu84620 2019-06-07 01:17
    关注

    You return the axios post but you also chain from it, If you want to do that then I'd recommend using the async/await pattern to clean the code up and avoid nested promise chaining. Here's a refactored look:

    async createSourceAndCustomer({ commit }, sourceData){
      try {
        commit('Loader/SET_LOADER', { status:1, message: 'Añadiendo forma de pago...' }, { root: true });
        const { data : { message } } = await axios.post('/stripe/create-source-and-customer', sourceData)
        commit('Loader/SET_LOADER', { status:2, message }, { root: true });
    
        return Promise.resolve()
    
      } catch (err) {
        commit('Loader/SET_LOADER', { status:3, errors: error, message: 'Oops, algo salio mal..' }, { root: true });
    
        return Promise.reject()
      }           
    },
    

    A bit cleaner for you, and it will resolve your issue.

    Edit

    If you didn't want to use this pattern, then you would instead wrap your code in a new promise, like this:

    createSourceAndCustomer({ commit }, sourceData)
        {
          return new Promise ((resolve, reject) => {
            commit('Loader/SET_LOADER', { status:1, message: 'Añadiendo forma de pago...' }, { root: true });
            axios.post('/stripe/create-source-and-customer', sourceData)
            .then((response) => {
                commit('Loader/SET_LOADER', { status:2, message: response.data.message }, { root: true });
                commit('CREATE_SOURCE_AND_CUSTOMER', response.data.customer);
                resolve()
            }, 
            (error) => {
                commit('Loader/SET_LOADER', { status:3, errors: error, message: 'Oops, algo salio mal..' }, { root: true });
                reject()
            });
          })
        },
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作