Take a look at the Mongoose documentation for exec()
and you will see:
aggregate.exec(callback);
// Because a promise is returned, the `callback` is optional.
var promise = aggregate.exec();
promise.then(..);
In other words, Mongoose's exec
can follow node callback style or use promises.
The first example you provided, Q.nbind()
assumes that exec
uses node callbacks. In your second example Q()
assumes that whatever passed into it is either a promise or a value.
The only reason both methods work is because Mongoose has implemented both node callbacks, and promises. This usually isn't the case!
Because Mongoose already supports promises natively, go for
var query = Kitty.find({ cute: true });
Q(query.exec()).done(/* ... */);
instead of wrapping the node style callbacks:
var findKitty = Q.nbind(Kitty.find, Kitty);
findKitty({ cute: true }).done(/* ... */);
To answer a question in the comments:
do you have any information on update and delete?
take a look at the queries documentation:
When a callback function:
- is passed, the operation will be executed immediately with the results
passed to the callback.
- is not passed, an instance of Query is
returned, which provides a special QueryBuilder interface for you.
So basically it's possible to do something like this:
var query = Kitty.update({ cute: false }); // :(
query.exec().then(/* */)
instead of using nbind:
var updateKitty = Q.nbind(Kitty.update, Kitty);
updateKitty({ cute: false }).then(/* */)
Both will work, it's up to you if you want to execute the update at that moment by using a callback, or in another line using exec
. Read that documentation for more information.