循环内发生意外的“等待”

时间:2018-11-20 15:52:37

标签: javascript node.js eslint

此代码块导致标题为eslint的规则出错。我试图弄清楚如何删除迭代依赖项,但我不太清楚。任何帮助将不胜感激。

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 });
}

1 个答案:

答案 0 :(得分:2)

您可以将诺言放入循环中,并在循环外使用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);