Skip to content

Instantly share code, notes, and snippets.

@codyromano
Created April 18, 2016 05:35
Show Gist options
  • Select an option

  • Save codyromano/597050a05ea99620e4edb29bd50aa2d7 to your computer and use it in GitHub Desktop.

Select an option

Save codyromano/597050a05ea99620e4edb29bd50aa2d7 to your computer and use it in GitHub Desktop.
function getMockDelay() {
return 1000 + (Math.random() * 4000);
}
async function getProducts() {
return new Promise((resolve) => {
var products = [
{title: 'Wave Runner', category: 'sports', cost: 5000},
{title: 'Mac', category: 'electronics', cost: 2000},
{title: 'Toaster', category: 'kitchen', cost: 10},
{title: 'Cereal', category: 'kitchen', cost: 5},
{title: 'Nike Shoes', category: 'clothing', cost: 50}
];
setTimeout(() => {resolve(products)}, getMockDelay());
});
}
async function getCustomerBalance() {
return new Promise((resolve) => {
let mockBalance = 4000;
setTimeout(() => resolve(mockBalance), getMockDelay());
});
}
async function getCustomerPrefs() {
return new Promise((resolve) => {
let preferences = ['electronics','clothing'];
setTimeout(() => resolve(preferences), getMockDelay());
});
}
(async function showRecommendations() {
let products = await getProducts();
let balance = await getBalance();
let prefs = await getCustomerPrefs();
let relevantProducts = products
.filter((product) => (product.cost <= balance))
.filter((product) => (prefs.indexOf(product.category) !== -1));
console.assert(relevantProducts.length === 1);
console.log('Relevant Products: %o', relevantProducts);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment