How could I bind `save` method using `Q` with mongoose
If you insist on Q, I'd do something like:
User.create = function(data){ var u = new User(data); u.save = Q.nfbind(testuser.save.bind(testuser)); // add other methods you want return u;};
However, I'd like to add that all other Mongoose methods already return a promise if you call .exec()
on them (so .find(..).exec()
returns an mpromise promise). That is, Mongoose already provides a promise API, save
is one of the only methods that are not included yet and there are plans to include it.
It's also beneficial to know that this becomes a one liner with Bluebird, which has a promisifyAll
function that creates promises that are easier to debug and are faster than Qs or Mongoose's.