Last active
March 3, 2017 07:38
-
-
Save hunterloftis/9fbec2a4c6e5480550351d67e3612045 to your computer and use it in GitHub Desktop.
async/await solution to the problem posed in https://medium.com/@bluepnume/even-with-async-await-you-probably-still-need-promises-9b259854c161#.kyz25n9mw
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
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() ] | |
} |
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
Cheese has a dependency on sauce:
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.