简体   繁体   English

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

[英]Promise then() on vuex action inside component

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. 一般来说,我喜欢将vuex动作的大部分逻辑保留在我的模块中,这样我保持组件清洁,大部分逻辑都聚集在模块中(这似乎是我的最佳形式),问题有时我需要做一些动作在动作之后的内部组件数据(通常是一个axios的承诺)结束(例如,在成功的ajax调用之后清除一个表单),我以为我通过在我的vuex动作调用中添加一个then闭包并在我的中返回axios promise来解决这个问题模块,但我注意到,然后闭包将永远解决,而不是只有当一切正常时,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: 我的vuex模块操作:

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. 总而言之,我希望clearForm方法只有在axios调用成功而不是总是触发时才会发生。

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. 你返回了axios帖子,但你也链接了它,如果你想这样做,那么我建议使用async/await模式清理代码并避免嵌套的promise链接。 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()
        });
      })
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM