I think i'm not understanding promises correctly. What I understand is that fetch()
returns a promise, I try to use the .then()
to return the value in the promise, but i always get undefined. What additionally confuses me is that I can console.log()
and it shows data, but when I try to use it elsewhere it returns undefined
. What am I doing wrong here?
var getData = function(){
var getRaceData = function()
{
fetch('http://ergast.com/api/f1/2008.json')
.then(function(response) {
response.json().then(function(data) {
var raceSeason = data.MRData.RaceTable;
var raceData = raceSeason.Races;
console.log(raceData);
// Not returning racedata here, always get undefined
return raceData;
});
}
)
.catch(function(err) {
return;
});
}
}