Why are you reconnecting to the db every time a user connects? If that is 
necessary, as in if the db connection depends on the user connecting, then 
define a function which given a query, connects to the db if not already 
connected, then executes the query after it connects. Then it disconnects 
from the db when the user disconnects.

Javascript isn't forcing you to nest your callbacks so much. It's flexible 
enough to let you define the ways you use asynchronous functions.

On Sunday, April 8, 2012 5:42:14 PM UTC-7, Matthew Hazlett wrote:
>
> I'm trying to write a simple app that preforms a db query when a user 
> connects to a tcp port.
>
> I can get the query working if I do everything as callbacks:
>
>      db.open(... fn() {
>          db.collection(.... fn() {
>               db.query(...... fn() {
>               });
>           });
>       });
>
> But as you can see this creates callback hell.
>
> What I would like to do is have a class, but being as everything is 
> async it makes it incredibly difficult to ensure all your variables are 
> set.
>
> var client;
> db.connect(connect, fn(){
>       client = connect;
> });
> client.close();
>
> Will cause an error because client hasn't been set yet.  Another thing I 
> thought of doing was chaining it all together:
>
> db.connect(connect, fn(){
>      ...
>     process.nextTick(fn(){ doNext(); });
> });
>
> this gets very messy and hard to manage, how can I deal with callback hell?
>
>
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to