简体   繁体   English

为什么在嵌套循环中等待而不是等待?

[英]Why in the nested loops await not wait?

In the nested loops I have async await.在嵌套循环中我有异步等待。 For me it is important that on each iteration loop will wait until await will be finished/data received and the start next iteration.对我来说,重要的是在每次迭代循环中等待直到 await 完成/接收到数据并开始下一次迭代。 Also this method used in the another loop.此方法也用于另一个循环。

const _sampleFilters = async (
  pageName,
  details,
  available,
) => {
  for (const filterName of Object.keys(details)) {
    for (const filterValue of Object.keys(details[filterName])) {
      const data = (await fetchDetails(
        pageName
      ));

      details[filterName][filterValue].allDetails = cloneDeep(
        getProcessedData(
          data.map(data => data.value),
          available,
          pageName,
        ),
      );
    }
  }
};
for (const item of items) {
   _sampleFilters();
}

Use await in your outermost loop.在最外层循环中使用 await 。 For example例如

async function foo()
{
    for (const item of items) {
       await _sampleFilters();
    }
}

foo();

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

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