I'm trying to extract information from a Promise returned by a fetch call. I don't seem to be able to do this. Either I get an undefined
or I get another Promise, rather then the actual content of the promise itself. For example:
async function fetchCoordinates(pluscode) {
let response = await fetch(url.replace("${pluscode}", pluscode));
return await response.json();
}
function getCoordinates(pluscode) {
let geoinfo;
fetchCoordinates(pluscode).then(info => {geoinfo = info})
return geoinfo
}
geoinfo = getCoordinates("{{ place.pluscode }}");
When evaluated, geoinfo is undefined
(instead of an object). However, if I change this line:
fetchCoordinates(pluscode).then(info => {geoinfo = info})
to:
fetchCoordinates(pluscode).then(info => {console.log(info)})
Then the object is saved logged correctly to the console.