Skip to content

Instantly share code, notes, and snippets.

@realyze
Last active February 14, 2018 19:40
Show Gist options
  • Save realyze/1430a7ff2f0e600b69d7e7e898ff78db to your computer and use it in GitHub Desktop.
Save realyze/1430a7ff2f0e600b69d7e7e898ff78db to your computer and use it in GitHub Desktop.
Pizza challenge
const defer = (res, ms) => new Promise(resolve => setTimeout(() => resolve(res), ms));
const makeDough = () => defer({add: () => {}}, 500);
const makeSauce = () => defer({determineCheese: () => 'cheddar'}, 250);
const grateCheese = () => defer({type: 'cheddar'}, 150);
async function makePizza(sauceType = 'red') {
const pDough = makeDough();
const pSauce = makeSauce(sauceType);
const pCheese = pSauce.then(sauce => grateCheese(sauce.determineCheese()));
const [dough, sauce, cheese] = await Promise.all([pDough, pSauce, pCheese]);
dough.add(sauce);
dough.add(cheese);
return dough;
}
makePizza();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment