最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Promise.all() to await the return of an object property - Stack Overflow

programmeradmin2浏览0评论

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

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:

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

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

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:

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

Share Improve this question edited Dec 7, 2020 at 1:58 Phil 165k25 gold badges262 silver badges267 bronze badges asked Dec 7, 2020 at 1:56 Wellington PerezWellington Perez 336 bronze badges 1
  • FYI, your sort parator can probably be return a.cities - b.cities – Phil Commented Dec 7, 2020 at 2:01
Add a ment  | 

2 Answers 2

Reset to default 8

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(
  states.map(
    state => lengthOfState(state.Sigla).then(cities => ({ state: state.Sigla, cities }))
  )
);
const NEW_LAND = 'newLand'
const ACTUAL_FINLAND = 'actualFinland'
const PIRKKAS_LAND = 'pirkkasLand'

const STATE_CITY_MAP = {
  [NEW_LAND]: ['HELSINKI', 'VANTAA', 'KORSO'],
  [ACTUAL_FINLAND]: ['TURKU'],
  [PIRKKAS_LAND]: ['WHITE RAPIDS', 'NOKIA'],
}

const mockGetCities = (stateName) => new Promise((res) => {
  setTimeout(() => { res([stateName, STATE_CITY_MAP[stateName]]) }, 0)
})

const pareStatesByCityQty = (a, b) => {
  if (a[1].length > b[1].length) return 1
  if (a[1].length < b[1].length) return -1
  return 0
}

const getSmallestStates = async (stateNames, cityQty) => {
  const cities = await Promise.all(stateNames.map(mockGetCities))
  return cities
    .sort(pareStatesByCityQty)
    .reverse()
    .slice(0, cityQty)
}

;(async () => {
  const stateNames = [NEW_LAND, ACTUAL_FINLAND, PIRKKAS_LAND]
  const smallestStates = await getSmallestStates(stateNames, 2)
  console.log(smallestStates)
})()
发布评论

评论列表(0)

  1. 暂无评论