简体   繁体   English

循环内意外的`await`

[英]Unexpected `await` inside a loop

This block of code is causing the titled eslint rule to error. 此代码块导致标题为eslint的规则出错。 I'm trying to figure out how to remove the iteration dependency but I'm not quite seeing it. 我试图弄清楚如何删除迭代依赖项,但我不太清楚。 Any help would be greatly appreciated. 任何帮助将不胜感激。

for (let i = 0; i < areaRes.hits.hits.length && i < MAX_PAGE_LIMIT; i += 1) {
  const record = areaRes.hits.hits[i];
  const snapshot = await listings.forBoundary(record, filter, listingType,
    status, roleId, roleType);
  areaResponseArr.push({ ...snapshot });
}

You could push the promises in your loop and use await outside of the loop, like: 您可以在循环中推送promise,并在循环外使用await ,例如:

const promises = [];
for (let i = 0; i < areaRes.hits.hits.length && i < MAX_PAGE_LIMIT; i += 1) {
  const record = areaRes.hits.hits[i];
  promises.push(listings.forBoundary(record, filter, listingType, status, roleId, roleType));
}
const areaResponseArr = await Promise.all(promises);

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

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