I’m relatively new to javascript and typescript in general and am struggling to understand the concept of async/await and Promises. (My knowledge is more Python based)
I am trying to make sense of why this function won’t work.
const getData = () => {
var myData: string;
axios.get(myEndPoint)
.then((response) => {
console.log(response.data) // this outputs the correct data
myData = response.data
return myData
})
.catch((error) => {
return error
});
And then outside this function I would like to get the contents of myData like so
const data_contents = getData()
However this would give me a value of undefined
I have tried various methods including changing the function getData() to an async function and declaring await for axios.get() however that would return a Promise {<pending>}
instead and not the actual contents of response.data
.
If someone could help explain what I’m doing wrong and point me in the right direction it would be really appreciated. Thanks a lot!