简体   繁体   English

使用带有 for in 循环的 async/await

[英]Using async/await with a for in loop

I have an Object that name is uploadedFiles.我有一个名为 uploadFiles 的 Object。 when I run this code first run console.log then run the for so I get the empty array.当我运行此代码时,首先运行 console.log 然后运行 for 所以我得到空数组。 how can I solve the problem我该如何解决这个问题

 let orderFilesData = [];
      for (let key in uploadedFiles) {
        uploadedFiles[key].map(async (file) => {
          let id = file.id;
          const orderFile = await this.orderFileRepository.findOne(id);
          orderFile.order = order;    
          await this.orderFileRepository.save(orderFile);
          orderFilesData.push(orderFile.fileUrl);
        });
      }
console.log(orderFilesData);

Since you do not return any data from the map, try using a foreach loop.由于您没有从 map 返回任何数据,因此请尝试使用foreach循环。 Since you use an async function, what you set in orderFilesData will be an array of promises, and you'll have to await them.由于您使用async function,因此您在orderFilesData中设置的内容将是一组承诺,您必须await它们。 The simplest solution is to use Promise.all the array (console.log(Promise.all(orderFilesData)) should do what you want)最简单的解决方案是使用Promise.all数组(console.log(Promise.all(orderFilesData))应该做你想做的事)

I suspect that the problem is that Array.map is async, so even though each one of the calls to save has await in front of it, iterating the elements and calling the anonymous function inside the .map is done in an async manner.我怀疑问题是Array.map是异步的,所以即使每个保存调用在它前面都有await ,迭代元素并在 .Z1D78DC8ED42449B51 内调用匿名.map以同步方式完成。

Try replacing uploadedFiles[key].map with a simple for loop and I believe that it'll fix the issue.尝试用一个简单for循环替换uploadedFiles[key].map ,我相信它会解决这个问题。

when array.map is used with async function it returns back a list of promises that is not runned.当 array.map 与异步 function 一起使用时,它返回一个未运行的承诺列表。 You'll have to start the process with Promise.All (or other).您必须使用Promise.All (或其他)开始该过程。

Try this inside your for loop在你的 for 循环中试试这个

 const uploadPromises = uploadedFiles[key].map(async (file) => {
       ...
  });
  await Promise.All(uploadPromises)

uploadedFiles seem to be an object, where the values of the keys are arrays? UploadFiles 似乎是uploadedFiles ,其中键的值是 arrays? So if you call uploadedFiles[key].map(...) you are creating an array of promises where each of them seems to be awaited.因此,如果您调用uploadedFiles[key].map(...)您正在创建一个承诺数组,其中每个承诺似乎都在等待。 But as the callback of map is asynchronous, you are in fact not awaiting.但是由于map的回调是异步的,实际上你并没有在等待。 The simplest would be using Promise.all() to await all promises in the array of promises resulting from map.最简单的方法是使用Promise.all()来等待由 map 产生的承诺数组中的所有承诺。

  let orderFilesData = [];
  for (let key in uploadedFiles) {
    await Promise.all(uploadedFiles[key].map(async (file) => {
      let id = file.id;
      const orderFile = await this.orderFileRepository.findOne(id);
      orderFile.order = order;    
      await this.orderFileRepository.save(orderFile);
      orderFilesData.push(orderFile.fileUrl);
    }));
  }
 console.log(orderFilesData);

But for this to work, make sure the surrounding function is async但要使其正常工作,请确保周围的 function 是async

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM