Before I render a page, I have to make several requests to mongodb. Here is how I do it now (db.js is my database processing layer):
db.findUser(objid, function(result1){
db.findAuthor(objid, function(result2){
db.findSmthElse (objid, function(result3){
res.render('template', {
user: result1,
author: result2,
smthelse: result2
});
});
});
});
I do suppose such methos kills the idea of async. However I need to call res.render only after all the requests are processed. How to launch it async? Should I use async library as it was advised here: multiple async mongo request in nodejs or are there any more suitable solutions? Thanks.
I do suppose such methos kills the idea of async
No they don't. That is precisely how asynchronous I/O is supposed to work. However, I do feel your pain with chaining multiple async calls. Before you know it, you can have 6 or 7 nested anonymous async functions.
There are two criteria I would use before I consider using a library like async or promise.
I hope this helps you make your decision