I have a complex workflow I'm trying to get working in highlandjs. What makes it complex is (1) you need to transform a list-of-futures to future-of-list and (2) it's a sequence of async calls where the 3rd call depends on both the output of the 1st and 2nd.
It's working in Scala. Checkout this gist: https://gist.github.com/frankandrobot/ad857698f680aceb44e8
Unfortunately, I haven't been able to convert #preprocess1 into the highland equivalent.
UPDATE: yea, I'm in highlandjs nested hell. So pointers on how to get out of there would help.
UPDATE: It turns I wasn't passing "targets" to preprocess(). Doh. It's working as expected.
The question is now how can I clean this up? Best practices? Something about it doesn't seem right.
This is the JS code:
//this spits out a list of tags
this._fetchAllTags = _.wrapCallback(db.fetchAllTags);
//this also spits out a list of tags
this._fetchCombinedNearestNeighbors = function(targets, alltags) {
var run_algorithm = _.wrapCallback(function(target, callback) {
db.findKNearestNeighbors(target, alltags, callback)
});
return _(targets)
.map(run_algorithm)
.parallel(4)
.sequence()
.collect()
.map(uniq); //get unique
};
//as you can see this depends on output of previous two async calls
this._fetchSSA = _.wrapCallback(function(allnearestneighbors, alltags, callback) {
db.fetchSSA(allnearestneighbors, alltags, callback)
});
//can we clean this up?
this.preprocess = function() {
return that._fetchAllTags()
.flatMap(function(alltags) {
return that._fetchCombinedNearestNeighbors(targets, alltags)
.flatMap(function (allneighbors) {
console.log('hi')
return that._fetchSSA(allneighbors, alltags);
});
});
};