So the first thing we're going to do is convert getExchangeRate and getCountries. The first step I'm going to take is to make this an async function, otherwise we can't use await. Then we're going to go ahead and set up a variable, a const response, and we are going to set this equal to await. Then we're going to await the following promise, the one that comes back from axios.get. I'm going to copy it, paste it, toss a semicolon at the end, and the only thing left to do is to return the value. I'm going to take the return statement and move it right there, then we can remove all of the previous code we had:
const getExchangeRate = async (from, to) => {
const response = await axios.get(`http://api.fixer.io/latest?base=${from}`);
return response.data.rates[to];
}
We now have the exact same functionality and it's a little bit nicer. Now, the benefit isn't as drastic as the benefit from going to convertCurrency to convertCurrencyAlt, but it is still indeed nice and I'd recommend using async anywhere you can. Now we need to convert getCountries using the exact same steps we just followed:
- Mark currencyCode one as async:
const getCountries = async (CurrencyCode) => {
- Create that response variable and actually awaiting on the promise. We're going to await the following promise:
axios.get(`https://restcountries.eu/rest/v2/currency/${currencyCode}`);
- The last step is to just return the exact same thing. There we go:
return response.data.map((country) => country.name);
Now that we have both of these converted, we're just going to test our work by saving the file and, as long as we get the exact same output, we'll move on to talking about errors; how it could catch them, how we can throw them, and how, in general, we can improve the errors that show up in our application.
Alright, the new result just showed up:

It is identical to the other two, which means that we are good to go. Now, I'd like to shift the discussion over to errors.