Q.ninvoke replacement in node bluebird Q.ninvoke replacement in node bluebird mongoose mongoose

Q.ninvoke replacement in node bluebird


Well, there are several things you can do. The most obvious is surprisingly "nothing".

Mongoose already returns Promises/A+ promises when you don't pass exec its callback, you can just assimilate them:

repo.count = function(entity,query)  { // entity is a mongoose model    return entity.find(query).count().exec(); // return promise};

You can safely use that promise with Bluebird which will assimilate it gladly:

Promise.resolve(repo.count(e, q)); // convert to bluebird promise explicitlysomeOtherBBPromise.then(function(query){    return repo.count(entity, query); // `then`able assimilation});

That said, it can be desirable to have bluebird promises all-over. For this bluebird has a very robust promisifyAll that lets you promisify mongoose at once:

var Promise = require("bluebird");var mongoose = Promise.promisifyAll(require("mongoose"));// everything on mongoose promisified someEntity.saveAsync(); // exists and returns bluebird promise


I come from google and the accepted answer is not I want. I am looking for a general replacement for ninvoke.

It should look something like this.

Q version:

Q.ninvoke(redisClient, "get", "foo")

Bluebird version:

Promise.promisify(redisClient.get, {context: redisClient})("foo")