forEach异步调用问题

在forEach中使用异步函数,会导致执行到await时跳出循环

解决办法:

  1、使用for循环

      const images: Array<object> = []
      for (const { originFileObj } of fileList) {
        images.push({ src: await getBase64(originFileObj) })
      }

  2、使用Promise.all与map结合

      const images = await Promise.all(
        fileList.map(async (item: any) => ({ src: await getBase64(item.originFileObj) }))
      )

 

 

 

 

参考:https://www.jianshu.com/p/2e5734425be9

posted @   吴小明-  阅读(480)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2020-03-11 vue知识点
2020-03-11 vue修饰符
点击右上角即可分享
微信分享提示