1.只保证开始和结束的顺序
业务场景 上传多张图片loading
console.log('开始上传')
async allUpload(images) { // images 是本地图片的缓存集合
// Promise.all 执行全部的异步完成之后 ,去循环image里面的即可
await Promise.all(images.map(async (v, index) => {
await this._UpLoadImage(v, index, images.length) // this._UpLoadImage 是单图上传或者其他的异步逻辑
}))
console.log('加载完毕...')
}
2.保证顺序的上传
// this.asyncForEach(images, this._UpLoadImage) 调用个方法
async asyncForEach(array, callback) { // 注意如果callback里面的有调用this的,已经被callback换成了上下文,或许会报空指针
for (let index = 0; index < array.length; index++) {
await callback(array[index]);
console.log(index)
}
}
网友评论