I have a really simple API, that grabs some data from a server, does a little processing and then sends it to the client. I'm trying to 'Promisify' this little module. Heres an example
var MyModule = function (){}
MyModule.prototype.makeRequest = function(requestURL){
//Set request up
//Return the promise for the request.
return request(requestOptions);
}
MyModule.prototype.myFunction = function(var1, var2, cb){
var URL = "http://....//";
this.makeRequest(URL)
.then(function(data)
{
//Some processing logic
cb(null,data);
})
.catch(function(err)
{
cb(err,null);
})
}
module.exports = MyModule;
Then to consume this module I want to do the following...
var MyModule = new(require('../lib/MyModule'));
MyModule.myFunction(var1,var2)
.then(function(data)
{
console.log(data);
}).catch(function(err){
console.log(err);
});
How can I get this to work using BlueBird? I've been experimenting with PromisifyAll() like so..
var MyModule = new(require('../lib/MyModule'));
var Promise = require("bluebird");
var MyModuleAsync = Promise.promisifyAll(MyModule);
My approach to this is obviously incorrect and I'm aware that I can create and return the promise manually inside the API but the docs suggest I shouldn't need to do this.