I would like to maintain an aggregate of intermediate results when executing a series of Q.all
tasks.
Concretely the problem is as follows:
var Obj = function(first, second) {
return {
stuff: first,
otherStuff: [1,2],
nextStuff: function() { return Q.fcall(function() { return second }) }
}
};
var ab = Q.fcall(function() { return Obj("A", "B"); });
var cd = Q.fcall(function() { return Obj("C", "D"); });
var promises = [ab, cd];
What I would like is a result of [{stuff: obj, nextStuff: "B"}, {stuff: obj, nextStuff: "D"}]
Right now I keep a reference to the previous result and map over them using underscore.js like so
Q.all(promises).then(function(stuffs) {
var promiseNextStuff = _.map(stuffs, function(x) { return x.nextStuff(); });
Q.all(promiseNextStuff).then(function(nextStuffs) {
var result = _.map(_.zip(stuffs, nextStuffs), function(obj) {
return _.object(["stuff", "nextStuff"], obj);
});
console.log("result", result);
});
});
... but I'm sure there is a better way in Q