Last active
April 18, 2016 19:02
-
-
Save thestephenmarshall/0e65d6a358c0f46bb6074d4d16d1397b to your computer and use it in GitHub Desktop.
Rancillio coffee maker script!
This file contains 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
class Rancilio { | |
turnOn() { | |
console.log('Turning the machine on...'); | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
console.log('Machine is on!'); | |
resolve(); | |
}, 1000); | |
}); | |
} | |
preHeat() { | |
console.log('Pre-heating...'); | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
console.log('Pre-heated!'); | |
resolve(); | |
}, 5000); | |
}); | |
} | |
onComplete() { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => {resolve(true)}, 1000); | |
}); | |
} | |
addBeans(beans) { | |
console.log(`Added ${beans.grams} grams of ground ${beans.type} beans to the hopper.`); | |
} | |
turnOnWater() { | |
console.log('Turning on water...'); | |
return new Promise((resolve, reject) => { | |
resolve(); | |
}) | |
} | |
brew() { | |
console.log('Brewing...'); | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
console.log('Done brewing!'); | |
resolve(); | |
}, 2500) | |
}); | |
} | |
turnOff() { | |
console.log('Rancillio is now off.'); | |
} | |
} | |
class Grinder { | |
grind(beans, corseness=10){ | |
return new Promise((resolve, reject) => { | |
console.log(`Grinding ${beans.grams} grams of ${beans.type} beans at ${corseness}...`); | |
resolve(beans); | |
}) | |
} | |
} | |
const coffeeMaker = new Rancilio(); | |
const grinder = new Grinder(); | |
const beans = {grams: 14, type: 'espresso'}; | |
async function prepare() { | |
console.log('Let\'s make some coffee!'); | |
await coffeeMaker.preHeat(); | |
return grinder.grind(beans).then(coffeeMaker.addBeans); | |
} | |
async function brew() { | |
return await coffeeMaker.turnOnWater().then(coffeeMaker.brew); | |
} | |
function cleanUp() { | |
console.log('Cleaning it up!'); | |
} | |
function drinkCoffee() { | |
console.log('Mmmmm.... Ohhhh.... Soooo gooood!'); | |
} | |
coffeeMaker.turnOn() | |
.then(prepare) | |
.then(brew) | |
.then(coffeeMaker.turnOff) | |
.then(cleanUp) | |
.then(drinkCoffee); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment