thelandog
thelandog

Reputation: 734

Vue JS - Call function after return

I have a function that lets me delete an account when a function deleteAccount is called. I want the user to be logged out straight after deleteAccount is returned from vuex store. Now I found a workaround for this by using setTimeout, however I don't feel as though this is really an optimal of doing it. Can someone kindly suggest a more optimal solution for this?

deleteAccount component

logout: function () {
    this.$store.commit(SET_LOGOUT);
    this.$store.commit(RESET_BASIC_MODAL_DATA);
    this.$router.push({name: ROUTE_NAMES_AUTH.LOGIN});
},
deleteAccount: function () {
    setTimeout(this.logout, 50);
    return this.$store.dispatch(DELETE_USER_ACCOUNT);
},

Upvotes: 0

Views: 376

Answers (1)

Jay Li
Jay Li

Reputation: 747

vuex actions return a promise, you can use then or await:

this.$store.dispatch(DELETE_USER_ACCOUNT).then(logout)
// or
deleteAccount: async function () {
  await this.$store.dispatch(DELETE_USER_ACCOUNT)
  logout()
}

Upvotes: 2

Related Questions