最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using asyncawait with a for in loop - Stack Overflow

programmeradmin3浏览0评论

I have an Object that name is uploadedFiles. when I run this code first run console.log then run the for so I get the empty array. 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);

I have an Object that name is uploadedFiles. when I run this code first run console.log then run the for so I get the empty array. 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);
Share Improve this question asked May 25, 2021 at 15:54 farzanmanafifarzanmanafi 3094 silver badges17 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

Since you do not return any data from the map, try using a foreach loop. Since you use an async function, what you set in orderFilesData will be an array of promises, and you'll have to await them. The simplest solution is to use Promise.all the array (console.log(Promise.all(orderFilesData)) should do what you want)

when array.map is used with async function it returns back a list of promises that is not runned. You'll have to start the process with Promise.all (or other).

Try this inside your for loop

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

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.

Try replacing uploadedFiles[key].map with a simple for loop and I believe that it'll fix the issue.

uploadedFiles seem to be an object, where the values of the keys are arrays? So if you call uploadedFiles[key].map(...) you are creating an array of promises where each of them seems to be awaited. But as the callback of map is asynchronous, you are in fact not awaiting. The simplest would be using Promise.all() to await all promises in the array of promises resulting from 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

发布评论

评论列表(0)

  1. 暂无评论