[英]Convert async.waterfall to Bacon
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. 我正在尝试将Baconjs集成到我当前的Nodejs项目中,在该项目中,我使用了Async https://github.com/caolan/async来处理回调。 One of the methods is async.waterfall that is now I want to port it to Bacon, using Bacon.fromNodeCallback(...). 方法之一是async.waterfall,现在我想使用Bacon.fromNodeCallback(...)将其移植到Bacon。 My code in NodeJS: 我在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! 谢谢!
Well, I finally solve my own problem after googling and reading this article http://abesto.net/bacon-js-on-the-server/ . 好吧,在谷歌搜索并阅读本文http://abesto.net/bacon-js-on-the-server/之后,我终于解决了自己的问题。 It makes me think about my problem and what I need to do with Bacon to archive the goal, rather then focus on converting async.waterfall method to Bacon. 这让我考虑了自己的问题以及如何与培根一起存档目标,而不是着重于将async.waterfall方法转换为培根。 So I post my code here, hope that someone finds it useful: 所以我在这里发布我的代码,希望有人觉得它有用:
var oldConditionStream = Bacon.fromNodeCallback(conditionBoundary,
"findById",
info.id).flatMap(function(oldCondition) {
if(oldCondition) {
return Bacon.once(oldCondition);
} else {
return Bacon.once(new Bacon.Error("fail"));
}
});
oldConditionStream.onError(function(error) {
return done(false, { message: "Failed to update condition" });
});
var updatingStream = oldConditionStream.flatMap(function(oldCondition) {
condition.created = util.parseDate(oldCondition.created, "YYYY-MM-DD h:mm:ss a");
return Bacon.fromNodeCallback(conditionBoundary, "updateCondition", condition).flatMap(function(nod) {
if(!nod) {
return Bacon.once(new Bacon.Error("fail"));
}
return Bacon.once(oldCondition);
});
});
updatingStream.onError(function(error) {
return done(false, util.error("Failed to update condition")("system"));
});
var updatingDescriptionStream = updatingStream.flatMap(function(oldCondition) {
if(!_.isEqual(oldCondition.prettyUrl, condition.prettyUrl)) {
var findingStream = Bacon.fromNodeCallback(conditionBoundary,
"findByPrettyUrlInDescription",
oldCondition.prettyUrl).flatMap(Bacon.fromArray);
var anotherStream = findingStream.flatMap(function(cond) {
var desc = cond.vietnamese.description;
desc = desc.replace(new RegExp(oldCondition.prettyUrl, "gmi"), condition.prettyUrl);
return Bacon.fromNodeCallback(conditionBoundary, "updateVietnameseDescription", cond.id, desc).map(true);
});
anotherStream.onEnd(function() {
return Bacon.once(true);
});
} else {
return Bacon.once(true);
}
});
updatingDescriptionStream.onValue(function(flag) {
return done(true, { message: "Condition has been updated" });
});
Cheer! 欢呼!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.