promise.all()
该方法用于将多个Promise
实例,包装成一个新的Promise
实例。
var p = Promise.all([p1,p2,p3]);
- 只有p1、p2、p3的状态都变成fulfilled(成功),p的状态才会变成fulfilled(成功),此时p1、p2、p3的返回值组成一个数组,传递给p的回调函数。
- 只要p1、p2、p3之中有一个被rejected(失败),p的状态就变成rejected(失败),此时第一个被reject的实例的返回值,会传递给p的回调函数
代码
Getlist(loadType = true) {
const promises = [this.$api.user.getMemberCardList, this.$api.user.getMemberCardList, this.$api.user
.getCashCardList, this.$api.user.getCashCardList].map((item, index) => {
let data = {}
if (index == 0 || index == 1) {
data = {
ctype: this.list[index].ctype
}
}
if (index == 2 || index == 3) {
data = {
type: this.list[index].ctype
}
}
return this.$request({
url: item,
method: "POST",
data: data,
loading: loadType
})
})
Promise.all(promises).then(values => {
//values 请求到的数据集合
values.forEach((item, index) => {
if (item.data.length > 0) {
item.data.forEach(myitem => {
})
}
})
this.list.forEach((item, index) => {
this.list[index].Pagedata = values[index].data
})
this.$nextTick(() => {
this.IsShow = true
})
uni.stopPullDownRefresh();
uni.hideLoading()
})
},