async beforeUpload() {
  this.loading = true;
  for (let file of this.fileList) {
    await this.upload(file);
  }
  // 队列上传完成,关闭loading
  this.loading = false;
}

// 具体上传逻辑
async upload() {
  // 上传的操作......
  // await......
  // 其它逻辑......
}

beforeUpload 是一个 async 函数,因此可以在其中使用 await
for 循环遍历 fileList 数组中的每个 file
await upload(file) 会等待 upload 函数返回的结果,然后再继续执行下一行代码。

在循环的时候,for...of会按照顺序进行遍历,循环内部使用await,每次upload函数执行完后for...of才会开启下一轮循环,直到循环完毕。
for...of可以实现Promise的队列调用,但前提是for...of所在的位置必须是async函数内部。


兔子先森
419 声望18 粉丝

致力于新技术的推广与优秀技术的普及。