File tree Expand file tree Collapse file tree 5 files changed +181
-0
lines changed
Expand file tree Collapse file tree 5 files changed +181
-0
lines changed Original file line number Diff line number Diff line change 1+ .vscode
2+ node_modules
Original file line number Diff line number Diff line change 1+ const users = [ {
2+ id : 1 ,
3+ name : 'Andrew' ,
4+ schoolId : 101
5+ } , {
6+ id : 2 ,
7+ name : 'Jessica' ,
8+ schoolId : 999
9+ } ] ;
10+
11+ const grades = [ {
12+ id : 1 ,
13+ schoolId : 101 ,
14+ grade : 86
15+ } , {
16+ id : 2 ,
17+ schoolId : 999 ,
18+ grade : 100
19+ } , {
20+ id : 3 ,
21+ schoolId : 101 ,
22+ grade : 80
23+ } ] ;
24+
25+ const getUser = ( id ) => {
26+ return new Promise ( ( resolve , reject ) => {
27+ const user = users . find ( ( user ) => user . id === id ) ;
28+
29+ if ( user ) {
30+ resolve ( user ) ;
31+ } else {
32+ reject ( `Unable to find user with id of ${ id } .` ) ;
33+ }
34+ } ) ;
35+ } ;
36+
37+ const getGrades = ( schoolId ) => {
38+ return new Promise ( ( resolve , reject ) => {
39+ resolve ( grades . filter ( ( grade ) => grade . schoolId === schoolId ) ) ;
40+ } ) ;
41+ } ;
42+
43+ // Andrew has a 83% in the class
44+ const getStatus = ( userId ) => {
45+ let user ;
46+ return getUser ( userId ) . then ( ( tempUser ) => {
47+ user = tempUser ;
48+ return getGrades ( user . schoolId ) ;
49+ } ) . then ( ( grades ) => {
50+ let average = 0 ;
51+
52+ if ( grades . length > 0 ) {
53+ average = grades . map ( ( grade ) => grade . grade ) . reduce ( ( a , b ) => a + b ) / grades . length ;
54+ }
55+
56+ return `${ user . name } has a ${ average } % in the class.` ;
57+ } ) ;
58+ } ;
59+
60+ const getStatusAlt = async ( userId ) => {
61+ const user = await getUser ( userId ) ;
62+ const grades = await getGrades ( user . schoolId ) ;
63+ let average = 0 ;
64+
65+ if ( grades . length > 0 ) {
66+ average = grades . map ( ( grade ) => grade . grade ) . reduce ( ( a , b ) => a + b ) / grades . length ;
67+ }
68+
69+ return `${ user . name } has a ${ average } % in the class.` ;
70+ } ;
71+
72+ getStatusAlt ( 2 ) . then ( ( status ) => {
73+ console . log ( status ) ;
74+ } ) . catch ( ( e ) => {
75+ console . log ( e ) ;
76+ } ) ;
77+
78+ // getStatus(123).then((status) => {
79+ // console.log(status);
80+ // }).catch((e) => {
81+ // console.log(e);
82+ // });
Original file line number Diff line number Diff line change 1+ const axios = require ( 'axios' ) ;
2+
3+ const getExchangeRate = async ( from , to ) => {
4+ try {
5+ const response = await axios . get ( 'http://data.fixer.io/api/latest?access_key=28fb091f0ecba07964b7843abea0f119' ) ;
6+ const euro = 1 / response . data . rates [ from ] ;
7+ const rate = euro * response . data . rates [ to ] ;
8+
9+ if ( isNaN ( rate ) ) {
10+ throw new Error ( ) ;
11+ }
12+
13+ return rate ;
14+ } catch ( e ) {
15+ throw new Error ( `Unable to get exchange rate for ${ from } and ${ to } .` ) ;
16+ }
17+ } ;
18+
19+ const getCountries = async ( currencyCode ) => {
20+ try {
21+ const response = await axios . get ( `https://restcountries.eu/rest/v2/currency/${ currencyCode } ` ) ;
22+ return response . data . map ( ( country ) => country . name ) ;
23+ } catch ( e ) {
24+ throw new Error ( `Unable to get countries that use ${ currencyCode } .` )
25+ }
26+ } ;
27+
28+ const convertCurrency = async ( from , to , amount ) => {
29+ const rate = await getExchangeRate ( from , to ) ;
30+ const countries = await getCountries ( to ) ;
31+ const convertedAmount = ( amount * rate ) . toFixed ( 2 ) ;
32+ return `${ amount } ${ from } is worth ${ convertedAmount } ${ to } . You can spend it in the following countries:\n ${ countries . join ( '\n ' ) } ` ;
33+ } ;
34+
35+ convertCurrency ( 'USD' , 'CAD' , 20 ) . then ( ( message ) => {
36+ console . log ( message ) ;
37+ } ) . catch ( ( e ) => {
38+ console . log ( e . message ) ;
39+ } ) ;
40+
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " aynsc-await-node" ,
3+ "version" : " 1.0.0" ,
4+ "description" : " " ,
5+ "main" : " app-promises.js" ,
6+ "scripts" : {
7+ "test" : " echo \" Error: no test specified\" && exit 1"
8+ },
9+ "author" : " " ,
10+ "license" : " ISC" ,
11+ "dependencies" : {
12+ "axios" : " ^0.18.0"
13+ }
14+ }
You can’t perform that action at this time.
0 commit comments