> 2. Coding async is weird at first, but you may grow to love it (as I have). Over the months, I've discovered many cool tricks and patterns that help keep the nesting to a minimum.
Can you share a few? Either here or as a blog post?
Can you share a few? Either here or as a blog post?
foo(..., function (n) {
// closure over q, r, s
});
Then what you can do is create a separate function, bar() that takes q, r, s as an argument, and returns a function: function bar(q, r, s) {
return function(n) {
// still has dependency on q, r, s
}
}
Once you have bar(), the foo() call above can be written as: foo(..., bar(q, r, s));
and you save one level of nesting. (This also makes the dependencies explicit, which is helpful.)The main loop of
https://github.com/ithinkihaveacat/node-fishback/blob/ce5f9c...
does quite a lot of this, if you want a real example.
function bar(n) {
// still has dependency on q, r, s
}
foo(..., bar);We use the waterfall most, as it allows one to run an array of function in a series with a callback at the end of the series.