async
async的用法,它作为一个关键字放到函数前面,用于表示函数是一个异步函数,因为async就是异步的意思,异步函数也就意味着该函数的执行不会阻塞后面代码的执行,async函数返回的是一个promise 对象。async 函数也是函数,平时我们怎么使用函数就怎么使用它,直接加括号 且 使用 .then的方法(参考https://www.jianshu.com/p/ab767311589d)
await
await的含义为等待。意思就是代码需要等待await后面的函数运行完并且有了返回结果之后,才继续执行下面的代码。这正是同步的效果,那么它在等待什么呢,它后面跟着什么呢?其实它后面可以放任何表达式,不过我们更多放的是一个promise对象的表达式(我立即是.then这种方式执行的)。注意await关键字,只能放在async函数里面!!!
methods: {
async initChart() {
this.chart = echarts.init(document.getElementById(this.id));
await fetchOverview().then(response => {
console.log('1--')
console.log(response.items)
console.log('2--')
this.option.series[0].data = response.items
}).catch(error => {
console.log(error);
})
console.log('a----')
console.log(this.option.series[0].data)
console.log('b-----')
this.chart.setOption(this.option);
},
}