es7 async和awit使用
请求依赖上一个请求结果,从之前的请求嵌套,修改为等待模式。
async function getBrand(){
return axios.get('/api/artificial_brand.json')
.then(function(res) {
let data = res.data;
return data
})
}
async function getDetail(){
return axios.get('/api/artificial_detail.json')
.then(function(res) {
let data = res.data;
return data
})
}
async function fn(){
console.log('1 await 前面')
let result1 = await getBrand();
let result2 = await getDetail();
// 并行加载getBrand和getDetail接口
// let [result1,degreeObj] = await Promise.all([this.getBrand(), this.getDetail()]);
console.log(result1)
console.log(result2)
console.log('3 await 后面')
}
fn();