在第一阶段,我获取数组,然后我用它来构建另一个查询到服务器。但是,在第二次获取操作之后,我无法访问此对象。
我可以看到表 - 但是当我尝试进行任何操作时 - 它返回undefined。
所以看起来fetchDetails函数还没有完成。
有人可以指导我如何实现这一目标,以便我可以在result
函数中fetchDetails
访问已解决的getData
吗?
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
}