Tuesday, 15 January 2013

Full async request to Mongodb via node.js -


Before I present a page, I have to encode several requests. Here's how I am doing it (db.js is my database processing level): 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 think that such Methos has killed Ascend's idea. However, after processing all the requests I must call res.render. How to start it async? Should I use the async library as told here: or is there a more suitable solution? Thank you.

I think this kind of mathos killed Ascend's idea

No, they do not do it properly that how Asynchronous I / O should work. However, I feel your pain while chaining multiple sync calls. Before you know this, you may have 6 or 7 nested anonymous async functions.

Before I consider using such a library, I will use two parameters like ASCIN or promise.

  1. Number of functions - This is something subjective, but how frustrating you look? You currently have 3 and if you want to stay for three, then another library is probably not worth the complication. Again, this is subjective and completely my opinion.
  2. Dependencies - If each method builds on the result of the previous one, they can not run in parallel. However, in your case, three database queries are independent and only res.render depends on the call chain function.

    I hope this will help you make your decision

No comments:

Post a Comment