【发布时间】:2017-12-20 19:52:16
【问题描述】:
我有 3 个函数要异步运行,当它们都完成后运行另一个函数:
app.get('/', function (req, res) {
var homePosts = {
newsest: [],
reviewed: [],
mostPopuler: [],
viewed: []
};
// fetch newest pages and asign the result to 'homePosts.newest '
function fetchNewestPages() {
Post.find({ "type": "public", "featuredImage": { "$exists": true } },"_id title briefDes featuredImage", function (err, posts) {
if (err) {
req.flash('error', 'An unknown error was occured.');
res.redirect('back');
} else {
homePosts.newsest = posts;
}
}).limit(4).sort( { date : -1 } );
}
// fetch most reviewed pages and asign the result to 'homePosts.reviewd '
function fetchMostReviewedPages() {
Post.find({ "type": "public", "featuredImage": { "$exists": true } },"_id title briefDes featuredImage", function (err, posts) {
if (err) {
req.flash('error', 'An unknown error was occured.');
res.redirect('back');
} else {
homePosts.reviewed = posts;
}
}).limit(4).sort( { commentsNumber : -1 } );
}
// fetch most popular pages and asign the result to 'homePosts.mostPopuler '
function fetchMostPopularPages() {
Post.find({ "type": "public", "featuredImage": { "$exists": true } },"_id title briefDes featuredImage", function (err, posts) {
if (err) {
req.flash('error', 'An unknown error was occured.');
res.redirect('back');
} else {
homePosts.mostPopuler = posts;
}
}).limit(4).sort( { likesNumber : -1 } );
}
// now run all 3 functions and when they are done render home page with the homePosts object which contains proper pages
async.parallel([
fetchNewestPages,
fetchMostReviewedPages,
fetchMostPopularPages
], function (err) { // it doesn't run at all
if (err) throw err;
console.log(homePosts);
res.render("home", {homePosts}); // render home page with the proper pages
});
});
希望您了解代码的作用,以下是代码作用的描述:
- 有 homePosts 对象,该对象将在主页上显示适当的页面
- 然后我们有一个函数将从数据库中获取 4 个最新页面,然后将它们分配给 homePost.newest
- 然后是一个函数,它将具有最多 cmets 的 4 个页面分配给 homePost.reviewed
- 第三个函数与上述两个函数一样,将最受欢迎的页面分配给 homePost.mostPopular
- 现在 async.js 应该完成它的工作,同时运行这 3 个函数,然后使用 homePosts 对象渲染一个主页,这是我有问题的部分
最后一个渲染主页的函数根本没有运行。我的问题在哪里?有什么方法可以同时运行这 3 个函数,然后运行最后一个渲染页面的函数?
更新:
我已经设法以这种方式做到这一点,但它们不是同时运行,而是一个接一个地运行。
// fetch newest pages and asign the result to 'homePosts.newest '
function fetchNewestPages(cb) {
Post.find({ "type": "public", "featuredImage": { "$exists": true } },"_id title briefDes featuredImage", function (err, posts) {
if (err) {
req.flash('error', 'An unknown error was occured.');
res.redirect('back');
} else {
homePosts.newsest = posts;
cb();
}
}).limit(4).sort( { date : -1 } );
}
// fetch most reviewed pages and asign the result to 'homePosts.reviewd '
function fetchMostReviewedPages(cb) {
Post.find({ "type": "public", "featuredImage": { "$exists": true } },"_id title briefDes featuredImage", function (err, posts) {
if (err) {
req.flash('error', 'An unknown error was occured.');
res.redirect('back');
} else {
homePosts.reviewed = posts;
cb();
}
}).limit(4).sort( { commentsNumber : -1 } );
}
// fetch most popular pages and asign the result to 'homePosts.mostPopuler '
function fetchMostPopularPages(cb) {
Post.find({ "type": "public", "featuredImage": { "$exists": true } },"_id title briefDes featuredImage", function (err, posts) {
if (err) {
req.flash('error', 'An unknown error was occured.');
res.redirect('back');
} else {
homePosts.mostPopuler = posts;
cb();
}
}).limit(4).sort( { likesNumber : -1 } );
}
fetchNewestPages(function () {
fetchMostReviewedPages(function () {
fetchMostPopularPages(function () {
res.render("home", {homePosts});
});
});
});
【问题讨论】:
标签: javascript node.js express asynchronous async.js