We are going to convert this over to async/await, and you're going to do that right here at the end of convertCurrency function. We're going to use Create convertCurrencyAlt as async function. So, just like we did over inside of app-promises, you're going to create an async function. Then you're going to fetch both pieces of data using await: Get countries and rate using await and our two functions. So, you're going to await both of these promises and then you're going to store that value in some variable. You can create a country's variable and a rate variable. Finally, you'll be able to take these two lines and just tack those on at the end:
const exchangedAmount = amount * rate;
return `${amount} ${from} is worth ${exchangedAmount} ${to}. ${to} can be used in the following countries: ${countries.join(', ')}`;
});
That will calculate the exchangedAmount and it will return the correct information: Calculate exchangedAmount and Return status string. You have two statements to get the data, one to calculate the exchanged amount and the final one to actually print things out.
We're going to go ahead and kick things off by creating that const convertCurrencyAlt. This one is going to be an async function, so we have to mark it as such. We can then move on to our arguments list, which is exactly the same as the other one: from, to, and amount. Then we're going to put the arrow and arrow function (=>), and we're going to open and close our curly braces.
const convertCurrencyAlt = async (from, to, amount) => {
});
Now we can move on to the first thing, which is getting the countries and getting the exchange rate. I'm going to start off with countries; const countries equals. We are going to await the promise that comes back from getCountries. What countries do we want to get? The ones where the to currency is able to be used. Then we're going to move on down below to rate. So, const rate. In this case, we are also trying to await something; we're trying to await the promise that comes back from getExchangeRate. We're trying to get the exchange rate, right here, from and to:
const convertCurrencyAlt = async (from, to, amount) => {
const countries = await getCountries(to);
const rate = await getExchangeRate(from, to);
};
So, at this point, we have all that data and we can move on to calculating the exchanged amount and returning the string. We already built that out, there's no need to recreate it. We can just copy both of those lines, paste them down below, and there we go. Everything is done:
const convertCurrencyAlt = async (from, to, amount) => {
const countries = await getCountries(to);
const rate = await getExchangeRate(from, to);
const exchangedAmount = amount * rate;
return `${amount} ${from} is worth ${exchangedAmount} ${to}. ${to} can be used in the following countries: ${countries.join(', ')}`;
};
Now, down below, instead of calling convertCurrency, we're able to call convertCurrencyAlt, passing in the exact same arguments and getting back the status.
convertCurrencyAlt('USD', 'CAD', 100).then((status) => {
console.log(status);
});
The difference is that our function is using async; a whole lot more readable, much easier to work with. We're going to go ahead and save currency-convert. That is going to run through the process of getting all of that data, converting it and then we're going to go ahead and print the status. What do we get at the end of the day? Over here, we get the exact same thing as shown in output here:

In the next section, we're going to talk about a few other places we can use async in this example, and we're also going to talk about how we can work with and handle errors.