├── .gitignore ├── .eslintrc.json ├── package.json ├── README.md └── currency-converter.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb-base" 3 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "currency-converter", 3 | "version": "1.0.0", 4 | "description": "Async/Await Currency Converter", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "Adrian Hajdin", 10 | "license": "ISC", 11 | "dependencies": { 12 | "axios": "^0.18.0" 13 | }, 14 | "devDependencies": { 15 | "eslint": "^5.10.0", 16 | "eslint-config-airbnb-base": "^13.1.0", 17 | "eslint-plugin-import": "^2.14.0" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Master Async/Await With This Real World Example 2 | ## Currency Converter Tutorial 3 | 4 | This is a code repository for the corresponding [article](https://medium.freecodecamp.org/how-to-master-async-await-with-this-real-world-example-19107e7558ad) on Medium. 5 | 6 | In this tutorial, we will build simple, but educational and useful application that is going to improve your overall knowledge of Async/Await. 7 | 8 | The program will take in currency code we want to convert from and currency code we want to convert to, as well as the amount of money, afterwards, the program will output correct exchange rate based on the data from the API’s. 9 | 10 | In this application we’re going receive data from two asynchronous sources: 11 | 1. Currency Layer — https://currencylayer.com  —  You’ll need to sign up for free so you can use API Access Key. This API will provide us with data needed to calculate exchange rate between currencies. 12 | 2. Rest Countries — http://restcountries.eu/  —  This API will give us information about where can we use the currency we just converted our money to. 13 | -------------------------------------------------------------------------------- /currency-converter.js: -------------------------------------------------------------------------------- 1 | const axios = require('axios'); 2 | 3 | const getExchangeRate = async (fromCurrency, toCurrency) => { 4 | try { 5 | const response = await axios.get('http://data.fixer.io/api/latest?access_key=f68b13604ac8e570a00f7d8fe7f25e1b&format=1'); 6 | 7 | const rate = response.data.rates; 8 | const euro = 1 / rate[fromCurrency]; 9 | const exchangeRate = euro * rate[toCurrency]; 10 | 11 | return exchangeRate; 12 | } catch (error) { 13 | throw new Error(`Unable to get currency ${fromCurrency} and ${toCurrency}`); 14 | } 15 | }; 16 | 17 | const getCountries = async (currencyCode) => { 18 | try { 19 | const response = await axios.get(`https://restcountries.eu/rest/v2/currency/${currencyCode}`); 20 | 21 | return response.data.map(country => country.name); 22 | } catch (error) { 23 | throw new Error(`Unable to get countries that use ${currencyCode}`); 24 | } 25 | }; 26 | 27 | const convertCurrency = async (fromCurrency, toCurrency, amount) => { 28 | let exchangeRate; 29 | let countries; 30 | 31 | await Promise.all([getExchangeRate(fromCurrency, toCurrency), getCountries(toCurrency)]) 32 | .then(([exchangeRateValue, countriesValue]) => { 33 | exchangeRate = exchangeRateValue; 34 | countries = countriesValue; 35 | }); 36 | 37 | const convertedAmount = (amount * exchangeRate).toFixed(2); 38 | 39 | return `${amount} ${fromCurrency} is worth ${convertedAmount} ${toCurrency}. You can spend these in the following countries: ${countries}`; 40 | }; 41 | 42 | convertCurrency('USD', 'HRK', 20) 43 | .then((message) => { 44 | console.log(message); 45 | }).catch((error) => { 46 | console.log(error.message); 47 | }); 48 | --------------------------------------------------------------------------------