Created
April 18, 2016 05:35
-
-
Save codyromano/597050a05ea99620e4edb29bd50aa2d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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