Skip to content

Instantly share code, notes, and snippets.

@hunterloftis
Last active March 3, 2017 07:38
Show Gist options
  • Save hunterloftis/9fbec2a4c6e5480550351d67e3612045 to your computer and use it in GitHub Desktop.
Save hunterloftis/9fbec2a4c6e5480550351d67e3612045 to your computer and use it in GitHub Desktop.
async function makePizza(sauceType = 'red') {
let [ dough, [ sauce, cheese ]] = await Promise.all([ makeDough(), makeToppings(sauceType) ])
return dough.add(sauce).add(cheese)
}
async function makeToppings(sauceType) {
return [ await makeSauce(sauceType), await makeCheese() ]
}
@bluepnume
Copy link

Cheese has a dependency on sauce:

async function makePizza(sauceType = 'red') {
  let [ dough, [ sauce, cheese ]] = await Promise.all([ makeDough(), makeToppings(sauceType) ])
  return dough.add(sauce).add(cheese)
}

async function makeToppings(sauceType) {
  let sauce = makeSauce(sauceType);
  let cheese = await grateCheese(sauce);
  return [ sauce, cheese ];
}

But otherwise, yep. I like this a lot. But I think the point still stands -- you have to think carefully about how to compose the underlying promises here, to come up with this solution.

@hunterloftis
Copy link
Author

hunterloftis commented Mar 3, 2017

Ah, thanks:

async function makePizza(sauceType = 'red') {
  let [ dough, [ sauce, cheese ]] = await Promise.all([ makeDough(), makeToppings(sauceType) ])
  return dough.add(sauce).add(cheese)
}

async function makeToppings(sauceType) {
  let sauce = await makeSauce(sauceType)
  return [ sauce, await makeCheese(sauce) ]
}

I agree, having an understanding of Promises is crucial.

Also, I'll grant this is a pretty simple pizza, and this solution would quickly get messy once more toppings were necessary with different interdependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment