I have a snippet of code which goes like this:
getLog (this is in a separate file)
export const getLog = async () => {
return client.then(async () => {
res1 = await method1()
res2 = await method2(res1)
// further parse data...
return res4
}
}
This function is async and it returns a promise which returns the result of various async calls.
getItem (React component)
const [items, setItems]=useState([])
getItem = async () => {
getLog().then((res: any) => {
let finalArr: any = []
res.map((val: any) => finalArr.push(val))
console.log(res, finalArr)
setItems(finalArr)
})
}
getItem()
res
: prints Array(0) but whe I expand it in DevTools it shows an array of objects:0: {name: "x", property:"y", otherProperty:"z"} 1: {name: "x", property:"y", otherProperty:"z"}
finalArr
: prints Array(0) and when expanded it's indeed an empty array
It seems to me this is an array-like object
(non-iterable).
How do I extract the data from this object?
What is interesting is that in React DevTools I can see the array is stored correctly in State but when I use {items.length}
in templates it prints 0
(I am unable to produce a minimal example without rebuilding the async/await flow with mock data.)