Q.ninvoke
, expects a Node.js style method. Node.js style methods accept a callback function and that function will be invoked with the error or with the result of the execution.
So, your program will work, if you can change your getInfo
function to accept a callback function and invoke it when the result has to be returned, like this
var Q = require('q');
function Apple(type) {
this.type = type;
this.color = "red";
this.getInfo = function(callback) {
return callback(null, this.color);
};
}
var apple = new Apple('macintosh');
apple.color = "reddish";
Q.ninvoke(apple, 'getInfo')
.then(function(color) {
console.log(color);
})
.fail(function(err) {
console.error(err);
});
Note: Since you are not using Node.js style method, you should use Q.invoke
instead of Q.ninvoke
like this
var Q = require('q');
function Apple(type) {
this.type = type;
this.color = "red";
this.getInfo = function() {
return this.color;
};
}
var apple = new Apple('macintosh');
apple.color = "reddish";
Q.invoke(apple, 'getInfo')
.then(function(color) {
console.log(color);
})
.fail(function(err) {
console.log(err);
});