A couple years ago I was experimenting with NodeJS and found that the "Step" library cleaned up some of my code rather nicely. When desiring to bring that code up to date, I noticed a few "red flags" on Step. (Not being updated for a couple of years, just 32 commits, etc.)
So I looked around and found Async.js, which has more features and active maintenance.
Looks nice and general. But I started trying to apply a transformation to use it instead, and might not be taking the right angle.
If I'm reading correctly, the core function of Step seems to be what Async.JS would call the "waterfall" pattern. So in Step you would write:
Step(
function firstStepNoArgs() {
foo.asyncCall(this);
},
function secondStep(err, argFromFoo) {
if (err) {
handleError(err);
}
bar.asyncCall(argFromFoo, 1, this.parallel());
baz.asyncCall(argFromFoo, 2, this.parallel());
},
function thirdStep(err, argFromBar, argFromBaz) {
if (err) {
handleError(err);
}
/* etc... */
}
);
If I didn't know any better, I might guess you'd do that in async.js like this (untested, consider it pseudocode; I'm talking about a theoretical change I haven't actually pursued yet)
function thirdStep(argFromBar, argFromBaz) {
/* etc... */
}
async.waterfall([
function firstStepNoArgs(callback) {
foo.asyncCall(callback);
},
function secondStep(argFromFoo, callback) {
async.parallel([
barResult: function(callback) {
bar.asyncCall(parameterFromFoo, 1, callback);
},
bazResult: function(callback) {
baz.asyncCall(parameterFromFoo, 2, callback);
}
],
function(err, result) {
if (err) {
handleError(err);
} else {
thirdStep(result.barResult, result.bazResult);
}
}
}
],
function(err, result) {
if (err) {
handleError(err);
} else {
/* no-op? just assume third-step runs? */
}
}
);
Step was very focused and sequential, and my little draft here shows it getting messy in the adaptation. Am I missing something?
So my question is: What is the right way to convert the clear Step code into Async.JS? Or have I chosen the wrong library to upgrade to? I don't want my code to get uglier, but I don't want to depend on a library that seems kind of "dead", either. :-/