Handling errors and awaiting async function
We're going to kick this section off by converting both getExchangeRate
and getCountries
over to async
functions. They're good candidates because we do have promises and we could just await those promises instead. Then we're going to talk about errors, how we can throw them and how we can customize errors that get thrown by other code. It's going to make it really useful and it'll make it a lot easier to actually use async
/await
in the real world where you do need to handle errors.
Converting getExchangeRate and getCountries into the async function
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...