Feb 25, 2017 · 1 min read
Promises already have an internal “memoize” feature. When using Promises, the asynchronous operation is not executed twice because we call .then two times.
My makePizza would be like the one Graham Realyze commented. But if I would prefer to avoid .then, then I’d probably write:
async function makePizza(sauceType = 'red') {
const pDough = makeDough();
const pSauce = makeSauce(sauceType);
const pCheese = (async () =>
grateCheese((await pSauce).determineCheese())
)(); const [dough, sauce, cheese] =
await Promise.all([pDough, pSauce, pCheese]); dough.add(sauce);
dough.add(cheese);
return dough;
}
