async/await摘要
ES7 提出的async 函数,终于让 JavaScript 对于异步操做有了终极解决方案。No more callback hell。
async 函数是 Generator 函数的语法糖。
复制代码
await 操做符用于等待一个 Promise 对象, 它只能在异步函数 async function 内部使用.
复制代码
async function 能够定义一个 异步函数。
复制代码
使用promise处理异步
getCLubs() {
this.ajax({
url: API.CLUBS,
method: 'get'
}).then(res => {
let clubs = res.data
clubs.map(item => {
item.sumPriceFormat = this.numberFormat(item.sumPrice / 10000, 2)
})
this.clubs = clubs
this.$apply()
})
}
onShow() {
this.getCLubs()
}
复制代码
使用async/await处理异步
//await只能在asyn函数中使用
async onLoad(options) {
let res = await this.ajax({
url: API.CLUBS,
method: 'get'
})
}
复制代码