JavaScript中async、await與try、catch
1、async、await是異步執行,async 里面后面的代碼會等 await 異步完再執行。
2、try 里有錯誤,執行 catch
async getSubCategoryData () {
try { // 放置的是可能出現異常的代碼塊
// 異步獲取數據
const res = await getSubCategories(this.cid)
// 將獲取到的子分類數據放置到 data 中
console.log('子數據:', res)
this.subs = res.categories
} catch (err) { // 放置的是出現異常后處理異常的代碼塊
console.log('異常:', err)
} finally {
// 不管是否出現異常,都會執行的代碼塊
}
}
