I can't get a Promise to return a string after having called .then on the fetch call.
I'm accessing a public API and retrieving the rates property from the JSON response.
{
"base": "EUR",
"rates": {
"CAD": 1.4867
},
"date": "2021-03-16"
}
I have a private method that calls fetch on this endpoint and returns a Promise type.
private getCadCurrency(): Promise<Currency> {
return fetch('https://api.ratesapi.io/api/latest?symbols=CAD')
.then(response => response.json())
.then(res => {
return res as Currency
});
}
Currency interface
interface Currency {
base: string,
rates: string,
date: string
}
When I make a call to getCadCurrency() and then chain it with a .then call I seem to still get Promise object instead of the Rate value.
let test = this.getCadCurrency().then(x => { return x.rates });
console.log(test);
When I inspect test in the Developer Tools I find that it reports a mixed state - probably when it's originally pending and then its final results.
What do I need to do to return the rates string?