changed to async.eachSeries

This commit is contained in:
Baris Usakli
2013-07-19 15:26:29 -04:00
parent c1b170ec86
commit ff9990701b
3 changed files with 26 additions and 25 deletions

View File

@@ -107,28 +107,29 @@ marked.setOptions({
}
Posts.getPostsByPids = function(pids, callback) {
var posts = [],
loaded = 0;
var posts = [];
for(var i=0, ii=pids.length; i<ii; ++i) {
(function(index, pid) {
Posts.getPostData(pid, function(postData) {
if(postData) {
postData.relativeTime = utils.relativeTime(postData.timestamp);
postData.post_rep = postData.reputation;
postData['edited-class'] = postData.editor !== '' ? '' : 'none';
postData['relativeEditTime'] = postData.edited !== '0' ? utils.relativeTime(postData.edited) : '';
postData.content = marked(postData.content || '');
posts[index] = postData;
}
++loaded;
if(loaded === pids.length)
callback(posts);
});
}(i, pids[i]));
function iterator(pid, callback) {
Posts.getPostData(pid, function(postData) {
if(postData) {
postData.relativeTime = utils.relativeTime(postData.timestamp);
postData.post_rep = postData.reputation;
postData['edited-class'] = postData.editor !== '' ? '' : 'none';
postData['relativeEditTime'] = postData.edited !== '0' ? utils.relativeTime(postData.edited) : '';
postData.content = marked(postData.content || '');
posts.push(postData);
}
callback(null);
});
}
async.eachSeries(pids, iterator, function(err) {
if(!err) {
callback(posts);
} else {
callback([]);
}
});
}
Posts.getPostField = function(pid, field, callback) {