Skip to content

Instantly share code, notes, and snippets.

@hawtim
Created September 1, 2022 10:16
Show Gist options
  • Save hawtim/fc68800ef3adeda15d8f5d254511ccb5 to your computer and use it in GitHub Desktop.
Save hawtim/fc68800ef3adeda15d8f5d254511ccb5 to your computer and use it in GitHub Desktop.
vue@2 lifecycle hook with async await
<template>
<div>
</div>
</template>
<script>
export default {
beforeCreate() {
console.log('beforeCreate hook done');
},
created() {
console.time('await')
Promise.all([
this.asyncFunctionA(),
this.asyncFunctionB(),
this.asyncFunctionC(),
])
console.timeEnd('await')
console.time('not await')
this.asyncFunctionA(),
this.asyncFunctionB(),
this.asyncFunctionC(),
console.timeEnd('not await')
console.log('created hook done');
},
beforeMount() {
console.log('beforeMount hook done');
},
mounted() {
console.log('mounted hook done');
},
methods: {
asyncFunctionA() {
return new Promise((resolve) => {
setTimeout(() => {
console.log('asyncFunctionA done');
resolve();
}, 1000);
});
},
asyncFunctionB() {
return new Promise((resolve) => {
setTimeout(() => {
console.log('asyncFunctionB done');
resolve();
}, 2000);
});
},
asyncFunctionC() {
return new Promise((resolve) => {
setTimeout(() => {
console.log('asyncFunctionC done');
resolve();
}, 1000);
});
},
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment