0

I have this function that should list the strings of available games into the activeGames array. When I log the array it returns empty, however, if I set a 1 second time out it will log correctly. Why is this happening?

let activeGames = []

const getActiveGames = async () => {
  const res = await fetch(someUrl)
  const data = await res.json()

  const list = data.content.filter(game => (
    game.aggregatorId === 'someString'
  ))

  activeGames = list.reduce((a, c) => {
    if (!a.includes(c)) {
      return [...a, c.gameId]
    } 
  }, [])
}

getActiveGames()

console.log(activeGames);
// []
Mark
  • 169
  • 1
  • 1
  • 10
  • 3
    You do not await the getActiveGames call – epascarello Feb 02 '21 at 16:51
  • 1
    Don't use global variables. You never know when they get written. Just move the `console.log` statement inside your function where you computed the `activeGames`! – Bergi Feb 02 '21 at 18:06

0 Answers0