I'm not sure whether it has been proposed here, but for such API, promises 
are good solution. In short with promises you encapsulate async state in 
object, so you just work with objects, no callbacks hell.
Your example when using promises may look like:

db = promisify(db.open).call(db, ...);
fooCollection = db.invokeAsync('collection', ....);
fooItems = fooCollection.invokeAsync('query', ..);

That's the way I work with mongodb driver for Node.js, if you find this 
attractive check https://github.com/medikoo/deferred

-- 
Mariusz Nowak
https://github.com/medikoo
http://twitter.com/medikoo

On Monday, April 9, 2012 2:42:14 AM UTC+2, 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