Skip to content

Instantly share code, notes, and snippets.

@codegino
Last active September 18, 2018 13:16
Show Gist options
  • Save codegino/3a93e5ea96971532635f9d74776bc937 to your computer and use it in GitHub Desktop.
Save codegino/3a93e5ea96971532635f9d74776bc937 to your computer and use it in GitHub Desktop.
Async Await Example
const users = [{
id: 1,
name: 'Carlo',
schoolId: 101
},{
id: 2,
name: 'Gino',
schoolId: 123
}];
const grades = [{
id: 1,
schoolId: 101,
grade: 99
}, {
id: 2,
schoolId: 123,
grade: 100
}, {
id: 3,
schoolId: 101,
grade: 90
}];
const getUser = (id) => {
return new Promise((resolve, reject) => {
const user = users.find(user => user.id === id);
if (user) {
resolve(user)
} else {
reject(`Unable to find user with id of ${id}.`)
}
});
};
const getGrades = (schoolId) => {
return new Promise((resolve) => {
resolve(grades.filter(grade => grade.schoolId === schoolId))
})
}
const getStatus = (userId) => {
let user;
return getUser(userId)
.then(tempUser => {
user = tempUser;
return getGrades(user.schoolId)
})
.then(grades => {
let average = 0;
if (grades.length > 0) {
average = grades.map(grade => grade.grade).reduce((a, b) => a + b) / grades.length;
}
return `${user.name} has a ${average}% in the class.`
})
}
getStatus(1)
.then((status) => {
console.log(status)
})
.catch(err => {
console.log(err)
})
getGrades(123)
.then(res => {
console.log(res)
})
getUser(2)
.then(res => {
console.log(res)
})
.catch(err => {
console.log(err)
})
const users = [{
id: 1,
name: 'Carlo',
schoolId: 101
},{
id: 2,
name: 'Gino',
schoolId: 123
}];
const grades = [{
id: 1,
schoolId: 101,
grade: 99
}, {
id: 2,
schoolId: 123,
grade: 100
}, {
id: 3,
schoolId: 101,
grade: 90
}];
const getUser = (id) => {
return new Promise((resolve, reject) => {
const user = users.find(user => user.id === id);
if (user) {
resolve(user)
} else {
reject(`Unable to find user with id of ${id}.`)
}
});
};
const getGrades = (schoolId) => {
return new Promise((resolve) => {
resolve(grades.filter(grade => grade.schoolId === schoolId))
})
}
const getStatus = async (userId) => {
const user = await getUser(userId);
const grades = await getGrades(user.schoolId);
let average = 0;
if (grades.length > 0) {
average = grades.map(grade => grade.grade).reduce((a, b) => a + b) / grades.length;
}
return `${user.name} has a ${average}% in the class.`
}
getStatus(1)
.then(name => console.log(name))
.catch(err => console.log(err))
const axios = require('axios');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
const getExchangeRate = async (from, to) => {
try {
const response = await axios.get('http://data.fixer.io/api/latest?access_key=955d2cec3e9fc16b4d5460de8a9be7ca')
const euro = 1 / response.data.rates[from];
const rate = euro * response.data.rates[to];
if (isNaN(rate)) {
throw new Error()
}
return rate
} catch (error) {
throw new Error(`Unable to get exchange rate for ${from} adn ${to}`);
}
};
const getCountries = async (currencyCode) => {
try {
const response = await axios.get(`https://restcountries.eu/rest/v2/currency/${currencyCode}`);
return response.data.map(country => country.name);
} catch (error) {
throw new Error(`Unable to get countries that use ${currencyCode}`);
}
}
const convertCurrency = async (from, to, amount) => {
const rate = await getExchangeRate(from, to)
const countries = await getCountries(to);
const convertedAmount = (rate * amount).toFixed(2)
return `${amount} ${from} is worth ${convertedAmount} ${to}. You can spend these in the following countries: ${countries.join(', ')}.`;
}
convertCurrency('USD', 'PHP', 20)
.then(res => {
console.log(res)
})
.catch(e => {
console.log(e.message)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment