小编Wel*_*rez的帖子

Promise.all() 等待对象属性的返回

在异步函数中,我有一个循环,在这个循环中,我需要使用 await 来解析来自另一个异步函数的承诺。

async function smallestCities(states) {
  const citiesInState = [];
  for (const state of states) {
    const length = await lengthOfState(state.Sigla);
    const stateObject = {
      state: state.Sigla,
      cities: length,
    };
    citiesInState.push(stateObject);
  }

  citiesInState.sort((a, b) => {
    if (a.cities > b.cities) return 1;
    if (a.cities < b.cities) return -1;
    return 0;
  });
  return citiesInState.filter((_, index) => index < 5).reverse();
}
Run Code Online (Sandbox Code Playgroud)

它工作正常,但 eslint 说不允许在循环内等待并使用 Promise.all() 来解决所有承诺。

问题是我的承诺在一个对象属性中:

函数的返回

我怎样才能弄清楚将 Promise.all() 与对象的属性一起使用?

javascript asynchronous promise async-await

2
推荐指数
1
解决办法
125
查看次数

标签 统计

async-await ×1

asynchronous ×1

javascript ×1

promise ×1