I'm facing a challenge with a piece of code I've been working on. Despite my efforts to find a solution online, I haven't had any success so far. Here is the code snippet in question:
const fetchSidebarData = async () => {
let dataArray = []
await axios.get('https://jsonplaceholder.typicode.com/posts').then(res => {
console.log('res1', res)
dataArray = res.data
}).catch(err => console.log(err))
console.log('dataArray', dataArray)
return dataArray
}
When I call this function like this:
const retrievedData = fetchSidebarData()
console.log('retrievedData', retrievedData)
The output in the console shows:
retrievedData Promise {<pending>}
__proto__: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: Array(100)
How can I store the PromiseResult in a variable? Any help would be greatly appreciated!