Using Node.js's Q library, when I try to pass an object prototype function to a .then
resolver in a promise, it's losing context of this
:
Foo.prototype.outsideResolve = function() {
var that = this;
return q.Promise(function(resolve, reject) {
console.log(that); // {data: "foo"}
resolve();
});
};
Foo.prototype.insideResolve = function() {
var that = this;
return q.Promise(function(resolve, reject) {
console.log(that); // undefined
resolve();
});
};
Foo.prototype.async = function() {
var foo = this;
return q.Promise(function(resolve, reject) {
foo.outsideResolve()
.then(foo.insideResolve)
.then(resolve)
.fail(reject)
.done();
});
};
Why does this happen? Is this expected behavior? Are you not supposed to be able to call prototype functions in promise resolves? Is there a way around this?