Created
September 1, 2022 10:16
-
-
Save hawtim/fc68800ef3adeda15d8f5d254511ccb5 to your computer and use it in GitHub Desktop.
vue@2 lifecycle hook with async await
This file contains hidden or 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
<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