I'm trying to retrieve data (currency rates) from open API with Fetch. Everything works fine and JSON object is successfully logged to console. However, I need that data to be accessible to all other functions as well. getCurrencyRates
function should return the JSON object, but it returns "undefined". What is the problem?
<script>
fetch('https://api.exchangeratesapi.io/latest')
.then(function(response){
return response.json();
}).then(function(json){
getCurrencyRates(json);
}).catch(function(error){
console.log(error);
});
// This function should return currency rates in JSON object
function getCurrencyRates(rates) {
console.log(rates);
return rates;
}
</script>