希望全部返回后再取消等待框,不知道怎么判断全部返回,百度了下,好像要用promise.all,不知道怎么写
//遍历数组,调用updata更新数据
ergodicArr(){
wx.showLoading({
title: '修改中',
})
this.data.obj.splice(0, this.data.obj.length) //清空数组
//遍历queryResult数组,mun值改变过则将该项添加进obj数组
for (var i = 0, len = this.data.queryResult.length;i<len;i++){
if (this.data.queryResult[i].num!=0){
this.data.obj.push(this.data.queryResult[i])
}
}
//遍历obj数组,调用obj云函数
for (var i = 0, len = this.data.obj.length;i<len;i++){
this.updata(this.data.obj[i])
}
},
//传入的id为字符串,需要改成数值型
updata(e){
wx.cloud.callFunction({
name:'changeData',
data:{
id:e.id*1,
obj:field,
value:e.num
}
}).then(res=>{
setTimeout(function () {
wx.hideLoading()
}).catch(err=>{
setTimeout(function () {
wx.hideLoading()
})
})
},
var promise3 = new Promise(function(resolve, reject) {
setTimeout(resolve, 100, 'foo');
});
这里的setTimeout(resolve, 100, 'foo');可以是任意一个函数吗?怎么传参
wx.showLoading({
title: '修改中...',
})
let pro = new Array(this.data.obj.length)
for (var i = 0, len = this.data.obj.length;i<len;i++){
//this.updata(this.data.obj[i])
pro[i] = new Promise((resolve, reject) => {
wx.cloud.callFunction({
name:'changeData',
data:{
obj: field,
value: this.data.obj[i].num,
id: this.data.obj[i].id * 1
},
success: res => {
resolve(res)
},
fail: err => {
reject(err)
}
})
})
}
console.log(pro)
Promise.all(pro).then(function (values){
wx.hideLoading()
})
这样写可以正确执行
参考
https://developers.weixin.qq.com/community/develop/doc/00082e3f538f704848a717f9d5c804
参考:
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise/all