javascript - Convert async.waterfall to Bacon -
I am trying to integrate backange in my existing node project, in this project, I used Async to handle callback. One of the methods is async.waterfall that I want to close it now, but it does not. Using Nod CallBack (...). My code NodeJS:
async.waterfall ([function findCondition (callback) {conditionBoundary.findById (condition.id, callback);} function updateCondition (loadedCondition, callback) {if (loadedCondition ) {condition.created = loadedCondition.created; conditionBoundary.updateCondition (position, function (error, NumberOfDocs) {if (error) {return callback (error);} if (loadedCondition.prettyUrl & amp; & amp ;! loadedCondition.prettyUrl == condition.prettyUrl) {return callback (no, really, LoadedCondition);} callback (zero, false, null);});}} function updateLinkInConditionDescription (isUpdated, loadedCondition, callback) {if (isUpdated) {conditionBoundary. FindByPrettyUrlInDescription (LoadedCondition.prettyUr L, function (error, docs) {if (error) {return callback (empty, wrong);} async.each (docs, function (doctor, each callback) {var desc = doc.v %%. Description = desc = Desc. Installer (new regExp (loaded condition prettyuvr, "gmi"), condition.prettyUrl); conditionbandi.update vendor AmeseDescription (doc.id, desc, each callback); }, Function (error) {callback (empty, inaccurate); }); }); } And {return callback (empty, true); }}, Function (error, result) {if (error) {returned (wrong, use.error ("failed to update status") ("system")); } Done (true, {message: "status has been updated"}); }); So I did it like this:
var result1 = Bacon.fromNodeCallback (fn, params ...); Results 1.onValue (function) (// some logic y result 2 = bacon. From nodecolalkback (FN2, Param ...); Results 2.onValue (function) () (Val)) {....} // And so on .. ..}); I feel like I am not doing it right, so what is the right way to accomplish this? I'm just getting my head with Bacon so that any help really can be appreciated. Thanks!
OK, I finally solve your problem after reading and reading this article. It focuses on me to change async.waterfall method to my problem is to think about the goal of the collection is required to do with bacon, instead bacon. So I do my code posted here, hope that there it finds useful:
var oldConditionStream = Bacon.fromNodeClallback (conditionboundary, "findById", info.id) .flatMap (function (Old condition) {if (old condition) {return bacon.Sone (old condition);} and {return bacon.one (new bacon error ("unsuccessful"))}}}); OldConditionStreamkonError (function (error) {return (false, {message: "Failed to update the status"})}}; 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) Kflatmap (function (clearance) {if Bacon.once (new bacon. error ( "failed"));} return bacon. old ();})}}; UpdatingStreamkonError (function (error) {a return (false, util.error ( "situation failed to update") ( "system") );}); Var updatingDescriptionStream = updatingStream.flatMap (function Urana condition) {if (! _Equal (chronic condition. PrettyUrl, condition.prettyUrl)) {var finding Stream = Bacon.fromNodeClallback (bet limits, "findByPrettyUrlInDescription", Old condition. 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 () {Bacon.once (true) return;});} else {return Bacon.once (true); }}); UpdatingDescriptionStream.onValue (function (flag) {returned (true, {message: "status has been updated"}}); Enthusiasm!
Comments
Post a Comment