Excellent points Daniel! I fear that these new asynchronous constructs promote inefficient patterns, and that we’ll see a lot of control flow written in a series that could be parallelized.
But at the same time, it can be difficult to write, maintain, and evolve code with complex control flow requirements.
This is why I created a helper function that I call evaluate (published as part of the Zousan extensions.)
Here is the pizza challenge written with it:
function makePizza(sauceType = 'red')
{
// Evaluate pizza ingredients
var ep = Zousan.evaluate(
{ name: "dough", deps: [], value: makeDough },
{ name: "sauce", deps: [], value: makeSauce(sauceType) },
{ name: "cheese", deps: ["sauce"], value: (sauce) => grateCheese(sauce.determineCheese())}
)
// make and return pizza
return ep.then(e => {
e.dough.add(e.sauce)
e.dough.add(e.cheese)
return e.dough
})
}Each resource has a name, valueand deps (dependencies). The value can be any value, a function which generates a value, or a promise that resolves to a value. The depsarray are the named resources that must be resolved before evaluation.
This is a declarative approach to manage such an evaluation with complex asynchronous control flow. It scales well to much longer lists of resources with intertwined dependencies.
