Tuesday, 27 August 2013

async each series does not return as expected

async each series does not return as expected

Im using async library, specifically the each method, but Im not getting
the expected result.
function instanceMethod (final_callback) {
obj = this;
async.parallel([
function (callback) {
getTopReplies (obj, function (err, result){
if (err) return callback(err);
if (result) {
obj.topReplies = result;
callback();
}
});
}, function (err){
if (err) return final_callback(err, false);
return final_callback (false, true);
});
This is the function Im calling from an async.parallel list of functions
function getTopReplies (obj, callback) {
mongoose.model('Post').find({replyTo: obj._id, draft: false})
.limit(3).sort('-createdAt')
.exec(function(err, tops){
if (err) return callback(err);
var result = [];
if (tops) {
async.eachSeries(tops, function(top, callback) {
result.push(top._id);
callback();
}, function (err){
if (err) return callback(err);
return callback(null, result);
});
}
});
}
theres a case where result should return two top posts id in the array but
it always returns 1 or if theres only 1 top reply to a post it returns
empty.
Is there anything wrong on my code?
Is the result array to be initiated somewhere else, like is it getting
reinitiated everytime, the each function is called or something?
Thank you!

No comments:

Post a Comment