这是ES7的语法,它是基于generator函数做的语法糖。
async function 函数名(){ let obj = await getName() return obj } 函数名().then(res=>{ //得到函数的返回值 })
<script src="./04数据.js"></script> </head> <body> </body> <script> // function* initData() { // let categoryList = yield getShopCategory() // let goods = yield getGoods(categoryList[0].id); // let comment = yield getComment(goods[0].id) // return comment // } async function initData(){ let categoryList = await getShopCategory() let goods = await getGoods(categoryList[0].id); let comment = await getComment(goods[0].id) return comment } initData().then(res=>{ console.log(res) })