-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7-tail-await.js
More file actions
34 lines (28 loc) · 785 Bytes
/
7-tail-await.js
File metadata and controls
34 lines (28 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';
const API_EXCHANGE = {
host: 'openexchangerates.org',
path: '/api/latest.json?app_id=',
key: '1f43ea96b1e343fe94333dd2b97a109d',
};
const getRate = async (currency) => {
const { host, path, key } = API_EXCHANGE;
const url = `https://${host}/${path}${key}`;
const res = await fetch(url);
const data = await res.json();
const rate = data.rates[currency];
return rate;
};
const exchange = async (value, rate) => {
if (rate < 0) throw new Error('Rate should be positive');
return value * rate;
};
const convert = async (value, currency) => {
const rate = await getRate(currency);
const result = await exchange(value, rate);
return result;
};
const main = async () => {
const res = await convert(150, 'UAH');
console.log(res);
};
main();