2
votes

I am trying to improve the readability of a biggish nodejs app that uses a lot of mongooose. The problem is that with a lot of dependent queries the callbacks get out of hand.

What are the practices of handling this issue?

1

1 Answers

2
votes

There are three common solutions for your problem.

First one is async.js lib.

Second one is using Promises. There are more then one implementation of promises in node.js. I know three implementations:

Third one is to use Fibers. There is fibers promise library that do all the tricky work for you.

There was plenty of similar questions before. For example, check this one.

All this libraries do the same thing - they make node.js asynchronous code pretty and readable. So, just choose one that looks simpler for you.

As for me, I prefer async.js lib.

Update: mongoose.js have its own build-in promise - mpromise. You can access it as mongoose.promise. But whenever you call exec() function on a query in mongoose it returns you a promise. I never actually used mongoose.js promises except for the REPL, but you may give it a try.