forEach 中使用await没用,不会等待异步

今天遇到一个问题,在forEach中不会等待异步完成

ActivityImgPath[this.activityName].forEach((path,index) => async () => {
            const key = ActivityImgName[this.activityName][index];
            await $asset.loadAsset(path, SpriteFrame).then(_asset => this.loadedAssets.set(key, _asset));
        });

 

 

只能改成for循环了

  for (let index = 0; index < ActivityImgPath[this.activityName].length; index++) {
    const path = ActivityImgPath[this.activityName][index];
    const key = ActivityImgName[this.activityName][index];
    const _asset = await $asset.loadAsset(path, SpriteFrame);
    this.loadedAssets.set(key, _asset);
  }

解决

posted @ 2023-11-21 17:58  赋流  阅读(81)  评论(0编辑  收藏  举报