I am new to Typescript React and also to Ionic framework.
I am using JS FETCH API and fetching data from a third-party. Now, i'd like to get the fetched data and return it to my DOM but I don't know on how to access the fetched data outside my Fetch.
please enlighten me.. thanks
function getData() {
fetch('URL')
.then(response => {
return response.json();
}).then(data_from_fetched => {
let data = data_from_fetched.results;
return data;
}}
let data = getData()
console.log(data); //undefined
let data = getData() console.log(data); //undefined
I tried this async too..
async function getData() {
fetch('url')
.then(response => {
return response.json();
}).then(data_from_fetched => {
console.log(data_from_fetched)
let data = data_from_fetched.results;
return data;
})
}
getData().then(data => console.log(data));
or this..
function getData() {
fetch('url')
.then(response => {
return response.json();
}).then(data_from_fetched => {
console.log(data_from_fetched)
let data = data_from_fetched.results;
return data;
})
}
let data= getData();
// and display it on my DOM
{movies.map(movie => movie){
let title = <IonCardTitle>{movie.title}</IonCardTitle>}}