Last active
January 27, 2019 10:17
-
-
Save dobromir-hristov/a85dc4f49a3212a60a19f57f5ac8513d to your computer and use it in GitHub Desktop.
Button that awaits promises automatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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