0

In the first phase, I fetch the array, which then I use to construct another query to the server. However, after this second fetch operation, I do not have access to this object.

I can see the tables - but when I try to do any operations - it returns undefined.

So it looks like fetchDetails function hasn't finished yet. Could someone please guide me how to make it happen so I can have access to resolved result from fetchDetails in getData function?

const URL = 'http://localhost:3000/stocks/';

async function getData() {
  let response = await fetch(URL)
  let avaibleStocks = await response.json()
  let symbols = avaibleStocks.stockSymbols
  let detailedRes = await fetchDetails(symbols)

  // here I would like to make next operations with data from detailedRes variable

  console.log(detailedRes) // I can see array with 'Value below was evaluated just now'
  console.log(detailedRes[0]) // but if I try to get any access I recive Undefined
}

async function fetchDetails(symbols) {
  let result = [];
   symbols.forEach( async s => {
    let res = await fetch(URL+s)
    let details = await res.json()
    await result.push(details)
  })
  return result
}
  • 1
    `push` does not return a promise, you should not `await` it. – Bergi Dec 07 '17 at 00:02
  • 1
    Using `await` for each sub request is pointless as it will make them take longer in aggregate since each doesn't get sent until prior one completes but none are dependent on each other Modified version that you can adapt https://jsfiddle.net/uq6n7p0w/ – charlietfl Dec 07 '17 at 00:19
  • Thank you very much for your help guys! – Michael Lester Dec 07 '17 at 00:55

0 Answers0