在多处调用Promise函数的时候,可以使用Promise
我应用的场景是当全部数据都加载之后才熏染出数据
wx.showLoading() 是小程序加载的图标
wx.hideLoading() 是小程序结束加载图标
三个请求的时候很难把握时候结束加载
const bid = options.bid
const detail = bookModel.getDetail(bid);
const comment = bookModel.getComment(bid);
const likeStatus = bookModel.getLikeStatus(bid);
detail.then(res=>{
console.log(res)
this.setData({
book:res
})
})
comment.then(res=>{
console.log(res)
this.setData({
comment:res.comments
})
})
likeStatus.then(res=>{
console.log(res)
this.setData({
likeStatus: res.like_status,
likeCount: res.fav_nums
})
})
使用Promise.all
wx.showLoading()//请求之前加载图标
Promise.all([detail,comment,likeStatus])
.then(res=>{
this.setData({
book:res[0],
comment:res[1].comments,
likeStatus:res[2].like_status,
likeCount: res[2].fav_nums
})
wx.hideLoading()//三个请求数据之后结束
})