if a function returns a promise, you can AWAIT the result of that promise!
const axios = require("axios");
const getCountry = async countryCode => {
try {
const response = await axios.get(
`https://restcountries.eu/rest/v2/currency/${countryCode}`
);
return response.data[0].name;
} catch (error) {
throw new Error("country code does not exist");
}
};
const getFlag = async capital => {
try {
const response = await axios.get(
`https://restcountries.eu/rest/v2/capital/${capital}`
);
return response.data[0].flag;
} catch (error) {
throw new Error("no such capital");
}
};
const getInfo = async (countryCode, capital) => {
const country = await getCountry(countryCode);
const flag = await getFlag(capital);
return {
info: {
country,
flag
}
};
};
getInfo("bgn", "sofia")
.then(info => {
console.log(info);
})
.catch(err => {
console.log(err);
});
const axios = require("axios");
async function displayInfo() {
// get album
const { data: album } = await axios.get(
"https://jsonplaceholder.typicode.com/albums/11"
);
// get todo
const { data: todo } = await axios.get(
"https://jsonplaceholder.typicode.com/todos/3"
);
// get user
const { data: user } = await axios.get(
"https://jsonplaceholder.typicode.com/users/4"
);
let info = {
todo,
user,
album
};
console.log(info);
}
displayInfo();