Skip to content

Instantly share code, notes, and snippets.

@dobromir-hristov
Last active January 27, 2019 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dobromir-hristov/a85dc4f49a3212a60a19f57f5ac8513d to your computer and use it in GitHub Desktop.
Save dobromir-hristov/a85dc4f49a3212a60a19f57f5ac8513d to your computer and use it in GitHub Desktop.
Button that awaits promises automatically
export default {
data:() => ({ posts: [] }),
methods: {
onClick(){
// return a promise from an api service
return this.$api.get('posts')
.then(r => {
this.posts = r.data
})
.catch(error => {
// You can handle the error, like show a notificaiton to the user
this.$notify.error('We could not fetch the posts, please try again')
// dont forget to re-throw the error, otherwise the promise will resolve successfully
throw error
})
},
// using async/await automatically returns the promise
async asyncAction(){
await this.performAsyncMethod()
await this.$store.dispatch('someAction')
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment