Skip to content

Commit

Permalink
And 'reduce' is improved
Browse files Browse the repository at this point in the history
  • Loading branch information
David Ellis committed Oct 8, 2013
1 parent d02d067 commit bbc6e21
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions lib/queue-flow.js
Expand Up @@ -354,26 +354,29 @@ Q.prototype.reduce = function reduce(callback, last, initial) {
var out = initial;
var outQueue;
if(!last) outQueue = new Q(null, this.options, this.namespace);
this.setHandlers(function(self, value, struct) {
if(isAsync(callback, 3)) {
var endFunc = function(end) {
if(!!last && last instanceof Function) last(out);
if(!!last && typeof(last) === 'string') q(last).push(out);
if(!last) {
outQueue.push(out);
outQueue[end]();
}
};
if(isAsync(callback, 3)) {
this.setHandlers(function(self, value, struct) {
callback(out, value, function(result) {
self.handlerCallback(struct, function() {
out = result;
});
});
} else {
}, endFunc);
} else {
this.setHandlers(function(self, value, struct) {
self.handlerCallback(struct, function() {
out = callback(out, value);
});
}
}, function(end) {
if(!!last && last instanceof Function) last(out);
if(!!last && typeof(last) === 'string') q(last).push(out);
if(!last) {
outQueue.push(out);
outQueue[end]();
}
});
}, endFunc);
}
return outQueue || this;
};
Q.prototype.reduceAsync = makeAsync(Q.prototype.reduce);
Expand Down

0 comments on commit bbc6e21

Please sign in to comment.