Skip to content

Promise.all() to await the return of an object property

Inside an async function i have a loop and inside this loop i need to use await to resolve a promise from another async function.

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.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();

It’s work fine, but eslint says to disallow await inside of loops and use Promise.all() to resolve all promises.

The problem is that my promises are in an object property:

return of the function

How can i figure out to use Promise.all() with properties of an object?



Chain a .then onto the lengthOfState call to make the whole Promise resolve to the object you need, inside the Promise.all:

const citiesInState = await Promise.all(
    state => lengthOfState(state.Sigla).then(cities => ({ state: state.Sigla, cities }))