简体   繁体   中英

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);
},

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()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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