First of all, three levels deep is not that bad at all. My personal threshold is around 5 levels. Also, despite being the author of half a dozen control flow libraries, I never use any of them anymore. I don't like the extra dependency on my library or app, and most importantly, I don't like all the inserted frames in my stack traces.
My favorite techniques are: - Function composition. Take common steps that involve two or more a sync layers and write common functions for then. Pass in all the needed state as arguments so they don't need to be nested. Or if there is a lot of needed state, next it in one or more layers, and pass in just some state. The trick is to find places where your business logic naturally form narrow points and cross there. - Serial actions I just nest. This is true especially if each step depends on state from a previous step. If it didn't depend on the outcome of a previous step, then why not do them in parallel? The natural closures that nesting provides gives you access to all the state you need. - For parallel work I just create a descriptive counter variable and call a common named `check` function that decrements the counter and checks if it should move on to the next named step function. The take away is less is more. 90% of the time you don't need a library at all. Just be aware of how closures and named functions work and take advantage of the tools the language already provides. - Tim Caswell On Apr 8, 2012 7:42 PM, "Matthew Hazlett" <[email protected]> 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<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 > nodejs+unsubscribe@**googlegroups.com<nodejs%[email protected]> > For more options, visit this group at > http://groups.google.com/**group/nodejs?hl=en?hl=en<http://groups.google.com/group/nodejs?hl=en?hl=en> > -- 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
