I'm trying to integrate Baconjs into my current Nodejs project, in this project I used Async https://github.com/caolan/async to handle callbacks. One of the methods is async.waterfall that is now I want to port it to Bacon, using Bacon.fromNodeCallback(...). My code in NodeJS:
async.waterfall([
function findCondition(callback) {
conditionBoundary.findById(condition.id, callback);
},
function updateCondition(loadedCondition, callback) {
if(loadedCondition) {
condition.created = loadedCondition.created;
conditionBoundary.updateCondition(condition, function(error, numberOfDocs) {
if(error) {
return callback(error);
}
if(loadedCondition.prettyUrl && loadedCondition.prettyUrl !== condition.prettyUrl) {
return callback(null, true, loadedCondition);
}
callback(null, false, null);
});
}
},
function updateLinkInConditionDescription(isUpdated, loadedCondition, callback) {
if(isUpdated) {
conditionBoundary.findByPrettyUrlInDescription(loadedCondition.prettyUrl, function(error, docs) {
if(error) {
return callback(null, false);
}
async.each(docs, function(doc, eachCallback) {
var desc = doc.vietnamese.description;
desc = desc.replace(new RegExp(loadedCondition.prettyUrl, "gmi"), condition.prettyUrl);
conditionBoundary.updateVietnameseDescription(doc.id, desc, eachCallback);
}, function(error) {
callback(null, false);
});
});
} else {
return callback(null, true);
}
}
], function(error, result) {
if(error) {
return done(false, util.error("Failed to update condition")("system"));
}
done(true, { message: "Condition has been updated" });
});
So I did it this way:
var result1 = Bacon.fromNodeCallback(fn, params...);
result1.onValue(function(val) {
// perform some logic
var result2 = Bacon.fromNodeCallback(fn2, params...);
result2.onValue(function(val)) { .... }
// and so on...
});
I feel like Im not doing it right. So what is the right way the accomplish this? I just get my head around with Bacon so any help can be really appreciated. Thanks!