我有3個功能,我想異步運行,當他們完成所有的工作運行另一個功能:如何使用async.js異步運行這3個node.js函數?
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
- 然後將分配4頁有mosts意見homePost.reviewed
- 第三功能,如2以上的功能分配最熱門的網頁,以homePost.mostPopular
- 現在async.js應該做的工作的功能,運行這些3個功能同時再渲染主頁與homePosts對象,這是一部分,我有問題
最後一個函數將呈現主頁沒有運行在所有。我的問題在哪裏?有什麼辦法可以同時運行這3個函數,然後運行最後一個會渲染頁面的函數嗎?
UPDATE:
我已經成功地做到這一點通過這種方式,但他們不是同時運行時,它們正在運行一個接一個。
// 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});
});
});
});
我這樣做:Promise.all( [fetchNewestPages, fetchMostReviewedPages, fetchMostPopularPages]) 。然後(RES =>的console.log(homePosts)) .catch(ERR =>的console.log(ERR))但homePosts是空的,這意味着函數沒有運行。 – Joseph
是否可以使用回調運行這些函數,以便我可以使用異步? – Joseph
homePosts爲空,因爲數據在該示例中以「res」返回。 Promise.all返回一個promise數組,所以試試:'Promise.all([fetchNewestPages,fetchMostReviewedPages,fetchMostPopularPages]).then((res)=> {console.log(res); console.log(res [0] ); console.log(res [1]); console.log(res [2]);}).catch((err)=> {console.log(err);})' – agm1984