I'm learning Vue.js right now and wanted to test out async await function using Vue instance.
I found weird bug where assigned data (number in this case) will result as 'undefined' when async await is triggered. Can anyone explain why this behavior is happening? Thanks.
<body>
<div id='wrapper'>
<div>{{ num }}</div>
<button @click='triggerr()'>{{ c }}</button>
</div>
<script>
new Vue({
el: '#wrapper',
data: {
num: 5,
c: 'click'
},
methods: {
settt(){
console.log(this.num); //outputs 5 (expected)
let abc = setInterval(()=>{
console.log(this.num); //outputs undefined (????)
this.num--;
if (this.num === 0){
clearInterval(abc);
return new Promise(function(res, rej){
res('Done!');
});
}
}, 1000);
},
async triggerr(){
let result = await this.settt();
this.num = result;
}
}
});
This is the full code if you want to see the error: https://www.w3schools.com/code/tryit.asp?filename=GHBBMXS1AR7P Demo of what it's suppose to do: https://www.w3schools.com/code/tryit.asp?filename=GHBBPY9BFK60